java 为什么 UrlValidator 对我的某些 Url 不起作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13288847/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 12:08:36  来源:igfitidea点击:

Why UrlValidator do not work for some of my Urls?

javaurl-validation

提问by dinesh707

String[] schemes = {"http","https"};
UrlValidator urlValidator = new UrlValidator(schemes, UrlValidator.ALLOW_ALL_SCHEMES);
System.out.println(urlValidator.isValid(myUrl));

the following URL says, invalid. Any one know why is that. the localnet is a localnetwork. But this works for any other public network (it seems).

以下网址说,无效。任何人都知道这是为什么。localnet 是一个本地网络。但这适用于任何其他公共网络(似乎)。

http://aunt.localnet/songs/barnbeat.ogg

采纳答案by Qwerky

As I thought, its failing on the top level;

正如我所想,它在顶级水平上失败了;

String topLevel = domainSegment[segmentCount - 1];
if (topLevel.length() < 2 || topLevel.length() > 4) {
  return false;
}

your top level is localnet.

你的最高级别是localnet.

回答by Quartz

The class you're using is deprecated. The replacement is

您正在使用的课程已被弃用。替换是

org.apache.commons.validator.routines.UrlValidator

org.apache.commons.validator.routines.UrlValidator

Which is more flexible. You can pass the flag ALLOW_LOCAL_URLS to the constructor which would allow most addresses like the one you are using. In our case, we had authentication data preceeding the address, so we had to use the even-more-flexible UrlValidator(RegexValidator authorityValidator, long options)constructor.

哪个更灵活。您可以将标志 ALLOW_LOCAL_URLS 传递给构造函数,这将允许大多数地址,例如您正在使用的地址。在我们的例子中,我们在地址之前有身份验证数据,所以我们不得不使用更加灵活的UrlValidator(RegexValidator authorityValidator, long options)构造函数。

回答by drchuck

This is fixed in the 1.4.1 release of the Apache Validator:

这在 Apache Validator 的 1.4.1 版本中得到修复:

https://issues.apache.org/jira/browse/VALIDATOR-342https://issues.apache.org/jira/browse/VALIDATOR/fixforversion/12320156

https://issues.apache.org/jira/browse/VALIDATOR-342 https://issues.apache.org/jira/browse/VALIDATOR/fixforversion/12320156

A simple upgrade to the latest version of the validator should fix things nicely.

简单升级到最新版本的验证器应该可以很好地解决问题。

回答by zmashiah

The library method fails on this URL:

库方法在此 URL 上失败:

  "http://en.wikipedia.org/wiki/3,2,1..._Frankie_Go_Boom"

Which is perfectly legal ([and existing) URL (try it out, the dialog of StackOverflow did not accept it, but if you do copy paste to your browser you will see a Wikipedia page)

这是完全合法的([和现有的)URL(试试看,StackOverflow 的对话框不接受它,但如果你复制粘贴到你的浏览器,你会看到一个维基百科页面)

I found by trial and error that the below code is more accurate:

我通过反复试验发现以下代码更准确:

public static boolean isValidURL(String url)
{
    URL u = null;
    try
    {
        u = new URL(url);
    }
    catch (MalformedURLException e)
    {
        return false;
    }

    try
    {
        u.toURI();
    }
    catch (URISyntaxException e)
    {  
        return false;  
    }  

    return true;  
}

回答by Armando Carrasco

You can use the following:

您可以使用以下内容:

UrlValidator urlValidator = new UrlValidator(schemes, new RegexValidator("^((?!-)[A-Za-z0-9-]{1,63}(?<!-)\.)+[A-Za-z]{2,6}$"), 0L);

回答by Mohamed Jameel

check line 2 it should be

检查第 2 行应该是

new UrlValidator(schemes);

if you want to allow 2 slashes and disallow fragments

如果你想允许 2 个斜线并禁止片段

new UrlValidator(schemes, ALLOW_2_SLASHES + NO_FRAGMENTS);

回答by Nikolay Kuznetsov

Here is the source code for isValid(String)method:

下面是isValid(String)方法的源代码:

You can check the result at each step by manual call to understand where it fails.

您可以通过手动调用检查每一步的结果,以了解失败的地方。