URL类常用于获取URL的某些信息,在处理URL相关信息时非常有用
常用构造器
public URL(String spec) throws MalformedURLException
指定URL字符串来完成URL对象的创建
1
| URL url = new URL("https://docs.spring.io/spring-boot/docs/current/reference/html/web.html#web");
|
getProtocol
获取此 URL的协议名称
public String getProtocol()
1 2 3 4
| URL url = new URL("https://docs.spring.io/spring-boot/docs/current/reference/html/web.html#web"); System.out.println(url.getProtocol());
|
getPort
返回此URL的端口号
public int getPort()
1 2 3 4
| URL url = new URL("http://localhost:8080"); System.out.println(url.getPort());
|
getDefaultPort
获得此URL的默认端口号
public int getDefaultPort()
1 2 3 4
| URL url = new URL("http://localhost:8080"); System.out.println(url.getDefaultPort());
|
getPath
获得此URL的路径
1 2 3 4
| URL url = new URL("https://docs.spring.io/spring-boot/docs/current/reference/html/web.html#web"); System.out.println(url.getPath());
|
getRef
获得此URL的锚点
1 2 3 4
| URL url = new URL("https://docs.spring.io/spring-boot/docs/current/reference/html/web.html#web"); System.out.println(url.getRef());
|
getUserInfo
获得此URL的UserInfo
1 2 3 4
| URL url = new URL("http://jack@qq.com"); System.out.println(url.getUserInfo());
|
getQuery
获得此URL的查询部分
1 2 3 4
| URL url = new URL("http://localhost?user=jack&password=123456"); System.out.println(url.getQuery());
|
getFile
获得此URL对应文件的绝对路径
1 2 3 4
| URL url = new URL("https://docs.spring.io/spring-boot/docs/current/reference/html/web.html#web"); System.out.println(url.getFile());
|
getHost
获得URL主机的ip或者域名,不返回端口
1 2 3 4
| URL url = new URL("https://localhost:8080"); System.out.println(url.getHost());
|
getAuthority
获得URL主机的ip或者域名,包括端口(如果有的话)
getAuthority()和getHost()函数之间的区别在于,getAuthority()随端口一起返回主机,而getHost()仅返回主机名
1 2 3 4
| URL url = new URL("https://localhost:8080"); System.out.println(url.getAuthority());
|
sameFile
比较两个URL的主体(域名/ip+路径+端口)是否相等
1 2 3 4 5
| URL url1 = new URL("https://baidu.com"); URL url2 = new URL("https://baidu.com:8080"); System.out.println(url1.sameFile(url2));
|
1 2 3 4 5
| URL url1 = new URL("https://baidu.com"); URL url2 = new URL("https://baidu.com#ikun"); System.out.println(url1.sameFile(url2));
|
获得此URL的字符串形式
public String toExternalForm()
1 2 3
| URL url = new URL("https://baidu.com"); System.out.println(url.toExternalForm());
|
openStream
请求此URL,并且返回响应body信息的InputStream流
public final InputStream openStream() throws java.io.IOException
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| URL url = new URL("https://baidu.com"); InputStream inputStream = url.openStream(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); String data = null; while ((data = bufferedReader.readLine()) != null) { System.out.println(data); }
|
getContent
用法同openStream,但是返回为Object对象,需要我们转化为InputStream对象
有两种重载形式
-
public final Object getContent() throws java.io.IOException
演示这个方法
-
public final Object getContent(Class[] classes) throws java.io.IOException
了解有这种重载形式即可
1 2 3 4 5 6 7 8
| URL url = new URL("https://baidu.com");
InputStream inputStream = (InputStream)url.getContent(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); String data = null; while ((data = bufferedReader.readLine()) != null) { System.out.println(data); }
|
openConnection
返回此URL的UrlConnection对象
有两种重载形式
-
public URLConnection openConnection() throws java.io.IOException
不使用代理创建UrlConnection对象
-
public URLConnection openConnection(Proxy proxy) throws java.io.IOException
使用代理创建UrlConnection对象
1 2
| URLConnection urlConnection = url.openConnection();
|
1 2 3 4 5
| URL url = new URL("https://baidu.com"); InetSocketAddress inetSocketAddress = new InetSocketAddress("127.0.0.1", 8080); Proxy proxy = new Proxy(Proxy.Type.HTTP, inetSocketAddress); URLConnection urlConnection = url.openConnection(proxy);
|
toURI
返回此URL的URI对象
public URI toURI() throws URISyntaxException
1 2 3
| URL url = new URL("https://baidu.com");
URI uri = url.toURI();
|