更新时间:2023-02-26 17:55:54
为了社区的利益,因为在搜索
url validator java 时,此主题在Google上排名第一
For the benefit of the community, since this thread is top on Google when searching for
"url validator java"
捕获异常代价很高,应尽可能避免。如果您只想验证String是否是有效的URL,则可以使用 UrlValidator > Apache Commons Validator 项目。
Catching exceptions is expensive, and should be avoided when possible. If you just want to verify your String is a valid URL, you can use the UrlValidator class from the Apache Commons Validator project.
例如:
String[] schemes = {"http","https"}; // DEFAULT schemes = "http", "https", "ftp"
UrlValidator urlValidator = new UrlValidator(schemes);
if (urlValidator.isValid("ftp://foo.bar.com/")) {
System.out.println("URL is valid");
} else {
System.out.println("URL is invalid");
}