1. Overview
1.概述
URL stands for Uniform Resource Locator, and it’s an address to a unique resource on the web.
URL代表统一资源定位器,它是网络上唯一资源的地址。。
In this tutorial, we’ll discuss the validation of URLs using Java. In modern web development, it’s very common to read, write, or access a URL through an application. Hence, a successful validation ensures a valid and compliant URL.
在本教程中,我们将讨论使用Java对URL进行验证。在现代网络开发中,通过应用程序读取、写入或访问一个URL是非常常见的。因此,一个成功的验证可以确保一个有效的、合规的URL。
There are different libraries that are used to validate a URL. We’ll discuss two classes – java.net.Url from the JDK and org.apache.commons.validator.routines.UrlValidator from the Apache Commons library.
有不同的库用于验证一个URL。我们将讨论两个类–来自JDK的java.net.Url和来自Apache Commons库的org.apache.commons.validator.routines.UrlValidator。
2. Validate URL Using JDK
2.使用JDK验证URL
Let’s see how we can validate a URL using the class java.net.URL:
让我们看看如何使用java.net.URL类来验证一个URL。
boolean isValidURL(String url) throws MalformedURLException, URISyntaxException {
try {
new URL(url).toURI();
return true;
} catch (MalformedURLException e) {
return false;
} catch (URISyntaxException e) {
return false;
}
}
In the above method, new URL(url).toURI(); tries to create an URI using the String parameter. If the String passed doesn’t qualify for the URL syntax, the library throws an Exception.
在上述方法中,new URL(url).toURI(); 试图使用String参数创建一个URI。如果传递的String不符合URL语法的要求,库就会抛出一个Exception。
The built-in URL class throws a MalformedURLException when it finds a malformed syntax in the input String object. When the format of the String isn’t compliant, the built-in class throws a URISyntaxException.
当内置的URL类在输入的String对象中发现畸形的语法时,会抛出一个MalformedURLException。当String的格式不符合要求时,内置类会抛出一个URISyntaxException.。
Now, let’s verify that our method works with a small test:
现在,让我们通过一个小测试来验证我们的方法是否有效。
assertTrue(isValidURL("http://baeldung.com/"));
assertFalse(isValidURL("https://www.baeldung.com/ java-%%$^&& iuyi"));
We must understand the difference between URL and URI. The toURI() method is important here as it ensures that any URL string that complies with RC 2396 is converted to URL. However, if we use new URL(String value), it won’t ensure that the URL created is fully compliant.
我们必须了解URL和URI之间的区别。 toURI() 方法在这里很重要,因为它确保任何符合RC 2396的URL字符串被转换为URL。然而,如果我们使用new URL(String value),就不能确保创建的URL是完全符合规定的。
Let’s see with an example that if we use only new URL(String url), many non-compliant URLs will pass the validation:
让我们通过一个例子来看看,如果我们只使用new URL(String url),许多不符合规定的URL将通过验证。
boolean isValidUrl(String url) throws MalformedURLException {
try {
// it will check only for scheme and not null input
new URL(url);
return true;
} catch (MalformedURLException e) {
return false;
}
}
Let’s see how the above method works for validating different URLs with some tests:
让我们看看上述方法是如何通过一些测试来验证不同的URL的。
assertTrue(isValidUrl("http://baeldung.com/"));
assertTrue(isValidUrl("https://www.baeldung.com/ java-%%$^&& iuyi"));
assertFalse(isValidUrl(""));
In the above method, new URL(url) only checks for the valid protocol and null string as input. Hence, if the protocol is correct, it will return a URL object even if it isn’t compliant with RC 2396.
在上述方法中,new URL(url)只检查有效的协议和空字符串作为输入。 因此,如果协议正确,它将返回一个URL对象,即使它不符合RC 2396。
Hence, we must use new URL(url).toURI() to ensure that the URL is valid and compliant.
因此,我们必须使用新的URL(url).toURI()来确保URL是有效和符合规定的。
3. Validate URL Using Apache Commons
3.使用Apache Commons验证URL
We need to import the commons-validator dependency into our pom.xml file:
我们需要将commons-validator依赖性导入我们的pom.xml文件。
<dependency>
<groupId>commons-validator</groupId>
<artifactId>commons-validator</artifactId>
<version>1.7</version>
</dependency>
Let’s use the UrlValidator class from this library to validate:
让我们使用这个库中的UrlValidator 类来进行验证。
boolean isValidURL(String url) throws MalformedURLException {
UrlValidator validator = new UrlValidator();
return validator.isValid(url);
}
In the above method, we create a URLValidator and then use the isValid() method to check for the URL validity of the String argument.
在上述方法中,我们创建一个URLValidator,然后使用isValid()方法来检查String参数的URL有效性。
Let’s check how the above method behaves for different inputs:
让我们检查一下上述方法对不同输入的表现。
assertFalse(isValidURL("https://www.baeldung.com/ java-%%$^&& iuyi"));
assertTrue(isValidURL("http://baeldung.com/"));
URLValidator allows us to finetune the conditions to validate a URL string. For instance, if we use the overloaded constructor UrlValidator(String[] schemes), it validates the URL only for the provided list of schemes (http, https, ftp, etc.).
URLValidator允许我们对验证一个URL字符串的条件进行微调。例如,如果我们使用重载的构造函数UrlValidator(String[] schemes),它只对所提供的方案列表(http、https、ftp等)验证URL。
Similarly, there are some other flags – ALLOW_2_SLASHES, NO_FRAGMENT, and ALLOW_ALL_SCHEMES that can be set as per the requirement. We can find the details for all the options provided by the library in the official documentation.
同样,还有一些其他的标志–ALLOW_2_SLASHES、NO_FRAGMENT,和ALLOW_ALL_SCHEMES,可以根据需求来设置。我们可以在官方文档中找到该库所提供的所有选项的详细信息。
4. Conclusion
4.总结
In this article, we learned two different ways to validate a URL. We also discussed the difference between URL(String url) and URL.toURI().
在这篇文章中,我们学习了验证URL的两种不同方法。我们还讨论了URL(String url) 和URL.toURI()之间的区别。
If we’ve to validate only the protocol and non-null string, then we can use the URL(String url) constructor. However, when we’ve to validate and pass the compliance check, we need to use URL(url).to URI().
如果我们只需要验证协议和非空字符串,那么我们可以使用URL(String url) 构造器。然而,当我们必须验证并通过合规性检查时,我们需要使用URL(url).to URI().。
Additionally, if we add the Apache Commons dependency, we can use the URLValidator class to perform the validation, and there are additional options available in the class to finetune the validation rules.
此外,如果我们添加Apache Commons依赖,我们可以使用URLValidator类来执行验证,并且在该类中有额外的选项可用于微调验证规则。
As always, the source code for the examples in this article is available over on GitHub.
一如既往,本文中的示例的源代码可在GitHub上获取。