开发者

How to verify if a String in Java is a valid URL?

How to verify if a String in J开发者_高级运维ava is a valid URL?


You can try to create a java.net.URL object out of it. If it is not a proper URL, a MalformedURLException will be thrown.


You can use UrlValidator from commons-validator. It will save you from writing code where the logic flow is guided by catching an exception, which is generally considered a bad practice. In this case, however, I think it's fine to do as others suggested, if you move this functionality to an utility method called isValidUrl(..)


For Android just add this line:

boolean isValid = URLUtil.isValidUrl( "your.uri" );


public static boolean isValidURL(String urlString) {
    try {
        URL url = new URL(urlString);
        url.toURI();
        return true;
    } catch (Exception e) {
        return false;
    }
}

This checks according to RFC2396, like <scheme>://<authority><path>?<query>#<fragment>, but scheme must be known by URL source code. These are not valid:

  • telnet://melvyl.ucop.edu
  • news:comp.infosystems.www.servers.unix
  • www.google.com


If you program in Android, you could use android.webkit.URLUtil to test.

URLUtil.isHttpUrl(url)
URLUtil.isHttpsUrl(url)

Hope it would be helpful.


Complementing Bozho answer, to go more practical:

  1. Download apache commons package and uncompress it.

    How to verify if a String in Java is a valid URL?

  2. Include commons-validator-1.4.0.jar in your java build path.
  3. Test it with this sample code (reference):

    //...your imports
    
    import org.apache.commons.validator.routines.*; // Import routines package!
    
    public static void main(String[] args){
    
    // Get an UrlValidator
    UrlValidator defaultValidator = new UrlValidator(); // default schemes
    if (defaultValidator.isValid("http://www.apache.org")) {
        System.out.println("valid");
    }
    if (!defaultValidator.isValid("http//www.oops.com")) {
        System.out.println("INvalid");
    }
    
    // Get an UrlValidator with custom schemes
    String[] customSchemes = { "sftp", "scp", "https" };
    UrlValidator customValidator = new UrlValidator(customSchemes);
    if (!customValidator.isValid("http://www.apache.org")) {
        System.out.println("valid");
    }
    
    // Get an UrlValidator that allows double slashes in the path
    UrlValidator doubleSlashValidator = new UrlValidator(UrlValidator.ALLOW_2_SLASHES);
    if (doubleSlashValidator.isValid("http://www.apache.org//projects")) {
        System.out.println("INvalid");
    }
    
  4. Run/Debug


This function validates a URL, and returns true (valid URL) or false (invalid URL).

public static boolean isURL(String url) {
    try {
        new URL(url);
        return true;
    } catch (Exception e) {
        return false;
    }
}

Be sure to import java.net.URL;


^(https://|http://|ftp://|ftps://)(?!-.)[^\\s/\$.?#].[^\\s]*$

This is a regex to validate URL for protocols http, https, ftp and ftps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜