如何在Java中检查有效的URL? [英] How to check for a valid URL in Java?

查看:498
本文介绍了如何在Java中检查有效的URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

检查URL在Java中是否有效的最佳方法是什么?

What is the best way to check if a URL is valid in Java?

如果试图调用new URL(urlString)并捕获到MalformedURLException,但对于以http://开头的任何内容似乎都感到满意.

If tried to call new URL(urlString) and catch a MalformedURLException, but it seems to be happy with anything that begins with http://.

我不关心建立连接,只是有效性.有办法吗? Hibernate Validator中的注释?我应该使用正则表达式吗?

I'm not concerned about establishing a connection, just validity. Is there a method for this? An annotation in Hibernate Validator? Should I use a regex?

编辑:接受的URL的一些示例为http://***http://my favorite site!.

Some examples of accepted URLs are http://*** and http://my favorite site!.

推荐答案

考虑使用

您可以设置一些属性来控制此类的行为,默认情况下,接受httphttpsftp.

There are several properties that you can set to control how this class behaves, by default http, https, and ftp are accepted.

这篇关于如何在Java中检查有效的URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆