url简单介绍:
https://blog.csdn.net/weixin_44146379
如上博客链接,其中https是scheme,更常用的说法是协议;blog.csdn.net是authority,也就是主机;/weixin_44146379/是path,也就是路径。
Java对Url进行判断:
使用URI这个类来判断一个网址的合法性是比较合适的,具体步骤如下:
先新建一个URI对象;
如果创建失败,则认为该URL不合法;
如果创建成功,再检查host是否为null;
如果host不为null,在检查协议是否是应用中能够处理的。
最终的代码如下:
public static boolean isValidUrl(String urlString) {
URI uri = null;
try {
uri = new URI(urlString);
} catch (URISyntaxException e) {
e.printStackTrace();
return false;
}
if (uri.getHost() == null) {
return false;
}
if (uri.getScheme().equalsIgnoreCase("http") || uri.getScheme().equalsIgnoreCase("https")) {
return true;
}
return false;
}
测试结果: