UrlValidator不适用于localhost

时间:2014-08-08 09:02:56

标签: java url-validation

我正面临一个问题,即我在代码中使用了UrlValidator。

UrlValidator urlValidator = UrlValidator.getInstance();
    if(urlValidator.isValid(url)) {
        throw new IllegalArgumentException( url + "not valid");
    }
 where url i'm passing is 

 [1]: http://localhost:8080/myapp/Education.html

。如果我通过url,我会收到IllegalArgumentException,即

 [2]: https://www.google.co.in/

它工作正常。如何使此代码适用于localhost:8080

1 个答案:

答案 0 :(得分:24)

如果您查看documentation,您将获得有关如何接受本地网址的提示

例如

public class Validator {
    public static void main(String[] args) {
        UrlValidator urlValidator = new UrlValidator(UrlValidator.ALLOW_LOCAL_URLS);
        if (urlValidator.isValid("http://localhost/page.htm")) {
            System.out.println("Valid URL");
        }
        else {
            System.out.println("Invalid URL");
        }

    }

}

将输出

Valid URL