Java URL处理

URL 类方法

在java.net包中定义了URL类,该类用来处理有关URL的内容。对于URL类的创建和使用,下面分别进行介绍。

java.net.URL提供了丰富的URL构建方式,并可以通过java.net.URL来获取资源。

序号方法描述
1public URL(String protocol, String host, int port, String file) throws MalformedURLException.
通过给定的参数(协议、主机名、端口号、文件名)创建URL。
2public URL(String protocol, String host, String file) throws MalformedURLException
使用指定的协议、主机名、文件名创建URL,端口使用协议的默认端口。
3public URL(String url) throws MalformedURLException
通过给定的URL字符串创建URL
4public URL(URL context, String url) throws MalformedURLException
使用基地址和相对URL创建
w+-3+-x+-u+-e.com提供本手册内容,请勿盗用!

URL类中包含了很多方法用于访问URL的各个部分,具体方法及描述如下:

序号方法描述
1public String getPath()
返回URL路径部分。
2public String getQuery()
返回URL查询部分。
3public String getAuthority()
获取此 URL 的授权部分。
4public int getPort()
返回URL端口部分
5public int getDefaultPort()
返回协议的默认端口号。
6public String getProtocol()
返回URL的协议
7public String getHost()
返回URL的主机
8public String getFile()
返回URL文件名部分
9public String getRef()
获取此 URL 的锚点(也称为"引用")。
10public URLConnection openConnection() throws IOException
打开一个URL连接,并运行客户端访问资源。
w+-3+-x+-u+-e.com提供本手册内容,请勿盗用!

URLConnection 方法

序号方法描述
1Object getContent()
检索URL链接内容
2Object getContent(Class[] classes)
检索URL链接内容
3String getContentEncoding()
返回头部 content-encoding 字段值。
4int getContentLength()
返回头部 content-length字段值
5String getContentType()
返回头部 content-type 字段值
6int getLastModified()
返回头部 last-modified 字段值。
7long getExpiration()
返回头部 expires 字段值。
8long getIfModifiedSince()
返回对象的 ifModifiedSince 字段值。
9public void setDoInput(boolean input)
URL 连接可用于输入和/或输出。如果打算使用 URL 连接进行输入,则将 DoInput 标志设置为 true;如果不打算使用,则设置为 false。默认值为 true。
10public void setDoOutput(boolean output)
URL 连接可用于输入和/或输出。如果打算使用 URL 连接进行输出,则将 DoOutput 标志设置为 true;如果不打算使用,则设置为 false。默认值为 false。
11public InputStream getInputStream() throws IOException
返回URL的输入流,用于读取资源
12public OutputStream getOutputStream() throws IOException
返回URL的输出流, 用于写入资源。
13public URL getURL()
返回 URLConnection 对象连接的URL
w+-3+-x+-u+-e.com提供本手册内容,请勿盗用!