URL类的使用

解析URL结构与方法
本文详细介绍了如何使用Java初始化URL并解析其各个组成部分,包括authority、query、path等,并通过具体示例展示了不同URL格式下各部分的具体表现。

转载自: http://hi.baidu.com/wy521ly/blog/item/861075d3e2f9e031970a169e.html

 

初始化一个url:

 

try {
url = new URL("
http://www.geelink.cn");
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

url的各种get方法:

    System.out.println("getAuthority:"+url.getAuthority());
System.out.println("getQuery:"+url.getQuery());
System.out.println("getPath:"+url.getPath());
System.out.println("getProtocol:"+url.getProtocol());
System.out.println("getFile:"+url.getFile());
System.out.println("getHost:"+url.getHost());
System.out.println("getRef:"+url.getRef());
System.out.println("getPort:"+url.getPort());

当url为:http://www.geelink.cn/index.html时,各种方法的返回值为
getAuthority:
www.geelink.cn
getQuery:
null
getPath:/index.html
getProtocol:http
getFile
:/index.html
getHost
:www.geelink.cn
getRef
:null
getPort:-1

当url为:http://www.geead.com/html/FAQ.html#paiqi时,各种方法的返值为:
getAuthority:www.geead.com
getQuery:null
getPath:/html/FAQ.html
getProtocol:http
getFile:/html/FAQ.html
getHost:www.geead.com
getRef:paiqi
getPort:-1

当url为:http://www.geead.com/index.php?option=com_user&view=reset时,各种方法的返回值为:
getAuthority:www.geead.com
getQuery:option=com_user&view=reset
getPath:/index.php
getProtocol:http
getFile:/index.php?option=com_user&view=reset
getHost:www.geead.com
getRef:null

getPort:-1

大家注意一下区别!另外getport如果没有特别设定的话默认返回-1!

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值