我有一个文本字段从用户那里获取位置信息(字符串类型)。它可以基于文件目录(例如 C:\directory
)或 Web URL(例如 http://localhost:8008/resources
)进行。系统将从该位置读取一些预定的元数据文件。
鉴于输入字符串,我如何有效地检测路径位置的性质是否是基于文件还是Web URL。
到目前为止我已经尝试过。
URL url = new URL(location); // will get MalformedURLException if it is a file based.
url.getProtocol().equalsIgnoreCase("http");
File file = new File(location); // will not hit exception if it is a url.
file.exist(); // return false if it is a url.
我仍在努力寻找处理这两种情况的最佳方法。 :-(
基本上,我不想使用前缀 http://
或 https://
显式地检查路径。
有没有一种优雅而恰当的方法来处理这个问题?