How to find out if the string path is a web url or file

I have a text box for getting location information (line type) from a user. This can be a file-based directory (e.g. C:\directory ) or a web URL (e.g. http://localhost:8008/resouces ). The system will read some predefined metadata files from the location.

Given the input string, how can I determine the nature of the location of the path, is it a file or a web address efficiently.

So far I have tried.

 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. 

I'm still trying to find a better way to handle both scenarios. :-(

Basically, I would not explicitly check the path using a prefix such as http:// or https://

Is there an elegant and proper way to do this?

+5
source share
4 answers

You can check if location starts with http:// or https:// :

 String s = location.trim().toLowerCase(); boolean isWeb = s.startsWith("http://") || s.startsWith("https://"); 

Or you can use the URI class instead of the URL , the URI does not throw a MalformedURLException as the URL class:

 URI u = new URI(location); boolean isWeb = "http".equalsIgnoreCase(u.getScheme()) || "https".equalsIgnoreCase(u.getScheme()) 

Although new URI() may also throw a URISyntaxException if you use a backslash in a location, for example. The best way is to either use a prefix check (my first sentence), or create a URL and catch MalformedURLException , which, if you quit, you will find out that it cannot be a valid web address.

+3
source

you may try:

 static public boolean isValidURL(String urlStr) { try { URI uri = new URI(urlStr); return uri.getScheme().equals("http") || uri.getScheme().equals("https"); } catch (Exception e) { return false; } } 

note that this will return false for any other reason that invalidates the url, or the url is not http / https: the invalid url is not necessarily the actual file name, and a good file name may refer to a non-existing version, therefore use it in conjunction with checking for a file.

+1
source

If you're open to using a try / catch script that is "elegant", here is a more specific way:

 try { processURL(new URL(location)); } catch (MalformedURLException ex){ File file = new File(location); if (file.exists()) { processFile(file); } else { throw new PersonalException("Can't find the file"); } } 

Thus, you get an automatic check of the syntax of the URLs and that it is not possible to check the presence of the file.

+1
source
 public boolean urlIsFile(String input) { if (input.startsWith("file:")) return true; try { return new File(input).exists(); } catch (Exception e) {return false;} } 

This is the best method because it is useless and will always return true if you have a link to the file. For example, other solutions do not allow and cannot cover the many available protocol schemes, such as ftp, sftp, scp, or any future protocol implementations. So this is one for all goals and purposes; with caution, the file must exist if it does not start with the file protocol.

if you look at the logic of a function by name, you should understand that returning false for a non-existent direct path search is not an error, i.e. a fact.

0
source

Source: https://habr.com/ru/post/1200005/


All Articles