java URL 种 获取 相关 参数

java URL 类 获取 相关 参数
通过URL类能够帮助我们获取url相关的一些参数。
import java.net.MalformedURLException;
import java.net.URL;

public class URLTest
{
	private static void testUrl(String path)
	{
		URL url = null;
		try
		{
			url = new URL(path);
		}
		catch (MalformedURLException e)
		{
			e.printStackTrace();
		}
		System.out.println("getAuthority:" + url.getAuthority());
		System.out.println("getDefaultPort:" + url.getDefaultPort());
		System.out.println("getFile:" + url.getFile());
		System.out.println("getHost:" + url.getHost());
		System.out.println("getPath:" + url.getPath());
		System.out.println("getPort:" + url.getPort());
		System.out.println("getProtocol:" + url.getProtocol());
		System.out.println("getQuery:" + url.getQuery());
		System.out.println("getRef:" + url.getRef());
		System.out.println("getUserInfo:" + url.getUserInfo());
	}
	public static void main(String[] args)
	{
		System.out.println("==http://www.baidu.com==");
		testUrl("http://www.baidu.com");
		System.out.println("==https://passport.baidu.com/==");
		testUrl("https://passport.baidu.com/");
	}
}


结果:
==http://www.baidu.com==
getAuthority:www.baidu.com
getDefaultPort:80
getFile:
getHost:www.baidu.com
getPath:
getPort:-1
getProtocol:http
getQuery:null
getRef:null
getUserInfo:null
==https://passport.baidu.com/==
getAuthority:passport.baidu.com
getDefaultPort:443
getFile:/
getHost:passport.baidu.com
getPath:/
getPort:-1
getProtocol:https
getQuery:null
getRef:null
getUserInfo:null