Java URL处理
URL 类方法
在java.net包中定义了URL类,该类用来处理有关URL的内容。对于URL类的创建和使用,下面分别进行介绍。
java.net.URL提供了丰富的URL构建方式,并可以通过java.net.URL来获取资源。
序号 | 方法描述 |
---|---|
1 | public URL(String protocol, String host, int port, String file) throws MalformedURLException. 通过给定的参数(协议、主机名、端口号、文件名)创建URL。 |
2 | public URL(String protocol, String host, String file) throws MalformedURLException 使用指定的协议、主机名、文件名创建URL,端口使用协议的默认端口。 |
3 | public URL(String url) throws MalformedURLException 通过给定的URL字符串创建URL |
4 | public URL(URL context, String url) throws MalformedURLException 使用基地址和相对URL创建 |
w+-3+-x+-u+-e.com提供本手册内容,请勿盗用!
URL类中包含了很多方法用于访问URL的各个部分,具体方法及描述如下:
序号 | 方法描述 |
---|---|
1 | public String getPath() 返回URL路径部分。 |
2 | public String getQuery() 返回URL查询部分。 |
3 | public String getAuthority() 获取此 URL 的授权部分。 |
4 | public int getPort() 返回URL端口部分 |
5 | public int getDefaultPort() 返回协议的默认端口号。 |
6 | public String getProtocol() 返回URL的协议 |
7 | public String getHost() 返回URL的主机 |
8 | public String getFile() 返回URL文件名部分 |
9 | public String getRef() 获取此 URL 的锚点(也称为"引用")。 |
10 | public URLConnection openConnection() throws IOException 打开一个URL连接,并运行客户端访问资源。 |
w+-3+-x+-u+-e.com提供本手册内容,请勿盗用!
URLConnection 方法
序号 | 方法描述 |
---|---|
1 | Object getContent() 检索URL链接内容 |
2 | Object getContent(Class[] classes) 检索URL链接内容 |
3 | String getContentEncoding() 返回头部 content-encoding 字段值。 |
4 | int getContentLength() 返回头部 content-length字段值 |
5 | String getContentType() 返回头部 content-type 字段值 |
6 | int getLastModified() 返回头部 last-modified 字段值。 |
7 | long getExpiration() 返回头部 expires 字段值。 |
8 | long getIfModifiedSince() 返回对象的 ifModifiedSince 字段值。 |
9 | public void setDoInput(boolean input) URL 连接可用于输入和/或输出。如果打算使用 URL 连接进行输入,则将 DoInput 标志设置为 true;如果不打算使用,则设置为 false。默认值为 true。 |
10 | public void setDoOutput(boolean output) URL 连接可用于输入和/或输出。如果打算使用 URL 连接进行输出,则将 DoOutput 标志设置为 true;如果不打算使用,则设置为 false。默认值为 false。 |
11 | public InputStream getInputStream() throws IOException 返回URL的输入流,用于读取资源 |
12 | public OutputStream getOutputStream() throws IOException 返回URL的输出流, 用于写入资源。 |
13 | public URL getURL() 返回 URLConnection 对象连接的URL |
w+-3+-x+-u+-e.com提供本手册内容,请勿盗用!