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());
// 输出:
// https

getPort

返回此URL的端口号

public int getPort()

1
2
3
4
URL url = new URL("http://localhost:8080");
System.out.println(url.getPort());
// 结果:
// 8080

getDefaultPort

获得此URL的默认端口号

public int getDefaultPort()

1
2
3
4
URL url = new URL("http://localhost:8080");
System.out.println(url.getDefaultPort());
// 输出结果:
// 80

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());
// 输出结果:
// /spring-boot/docs/current/reference/html/web.html

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());
// 输出结果:
// web

getUserInfo

获得此URL的UserInfo

1
2
3
4
URL url = new URL("http://jack@qq.com");
System.out.println(url.getUserInfo());
// 输出结果:
// jack

getQuery

获得此URL的查询部分

1
2
3
4
URL url = new URL("http://localhost?user=jack&password=123456");
System.out.println(url.getQuery());
// 输出
// user=jack&password=123456

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());
// 输出结果:
// /spring-boot/docs/current/reference/html/web.html

getHost

获得URL主机的ip或者域名,不返回端口

1
2
3
4
URL url = new URL("https://localhost:8080");
System.out.println(url.getHost());
// 输出结果:
// localhost

getAuthority

获得URL主机的ip或者域名,包括端口(如果有的话)

getAuthority()和getHost()函数之间的区别在于,getAuthority()随端口一起返回主机,而getHost()仅返回主机名

1
2
3
4
URL url = new URL("https://localhost:8080");
System.out.println(url.getAuthority());
// 输出结果:
// localhost:8080

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));
// 输出结果:
// false
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));
// 输出结果:
// true

toExternalForm

获得此URL的字符串形式

public String toExternalForm()

1
2
3
URL url = new URL("https://baidu.com");
System.out.println(url.toExternalForm());
// https://baidu.com

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);
}
// 输出结果
// <html>
// <head><title>302 Found</title></head>
// <body bgcolor="white">
// <center><h1>302 Found</h1></center>
// <hr><center>bfe/1.0.8.18</center>
// </body>
// </html>

getContent

用法同openStream,但是返回为Object对象,需要我们转化为InputStream对象

有两种重载形式

  1. public final Object getContent() throws java.io.IOException

    演示这个方法

  2. 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");
// Object向下转型为InputStream
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对象

有两种重载形式

  1. public URLConnection openConnection() throws java.io.IOException

    不使用代理创建UrlConnection对象

  2. public URLConnection openConnection(Proxy proxy) throws java.io.IOException

​ 使用代理创建UrlConnection对象

1
2
// 不使用代理,直接返回URLConnection对象
URLConnection urlConnection = url.openConnection();
1
2
3
4
5
// 使用代理,创建URLConnection对象
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");
// 返回此URL的URI对象
URI uri = url.toURI();