如何验证 Java 中的字符串是否为有效 URL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3931696/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to verify if a String in Java is a valid URL
提问by lacas
How can I check if a string is a URL in Java?
如何检查字符串是否是 Java 中的 URL?
采纳答案by Grodriguez
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.
您可以尝试从中创建一个java.net.URL
对象。如果它不是正确的 URL,MalformedURLException
则会抛出 a。
回答by Bozho
You can use UrlValidator
from commons-validator. It will save you from writing code where the logic flow is guided by caching 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(..)
您可以UrlValidator
从commons-validator 使用。它将避免您编写通过缓存异常来引导逻辑流的代码,这通常被认为是一种不好的做法。但是,在这种情况下,如果您将此功能移动到名为isValidUrl(..)
回答by Esteban Cacavelos
Complementing Bozho answer, to go more practical:
补充Bozho的答案,更实用:
- Download apache commons packageand uncompress it.
- Include commons-validator-1.4.0.jar in your java build path.
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"); }
Run/Debug
- 下载 apache commons包并解压。
- 在您的 java 构建路径中包含 commons-validator-1.4.0.jar。
使用此示例代码(参考)对其进行测试:
//...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"); }
运行/调试
回答by alijandro
If you program in Android, you could use android.webkit.URLUtil
to test.
如果你在Android 中编程,你可以android.webkit.URLUtil
用来测试。
URLUtil.isHttpUrl(url)
URLUtil.isHttpsUrl(url)
Hope it would be helpful.
希望它会有所帮助。
回答by Aaron Esau
This function validates a URL, and returns true (valid URL) or false (invalid URL).
此函数验证 URL,并返回 true(有效 URL)或 false(无效 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;
务必 import java.net.URL;
回答by Rafa0809
For Androidjust add this line:
对于Android,只需添加以下行:
boolean isValid = URLUtil.isValidUrl( "your.uri" );
回答by BullyWiiPlaza
Here you go:
干得好:
public static boolean isValidURL(String urlString)
{
try
{
URL url = new URL(urlString);
url.toURI();
return true;
} catch (Exception exception)
{
return false;
}
}
回答by Saurabh Tamrakar
^(https://|http://|ftp://|ftps://)(?!-.)[^\s/$.?#].[^\s]*$
This is a regex to validate URL for protocols http, https, ftp and ftps.
这是一个用于验证 http、https、ftp 和 ftps 协议 URL 的正则表达式。