您的位置:首页 > 编程语言 > Java开发

java network programming读书笔记(3) URL类和URI类

2011-01-03 10:58 148 查看
URL 类的java文档大概分为三部分:

1 构造URL 通过设置URL的scheme, authroity, path 等

2 当URL构造好后可以用各种getter 来获得它的各部分

3 从URL中直接得到流,下面是几种方法的详解

public final InputStream openStream( ) throws IOException

此方法得到的是原始的字节流,没有包括http协议的头。具体内容如下:

The openStream( ) method connects to the resource referenced by the URL, performs any necessary handshaking between the client and the server, and returns an InputStream from which data can be read. The data you get from this InputStream is the raw (i.e., uninterpreted) contents of the file the URL references: ASCII if you're reading an ASCII text file, raw HTML if you're reading an HTML file, binary image data if you're reading an image file, and so forth. It does not include any of the HTTP headers or any other protocol-related information. You can read from this InputStream as you would read from any other InputStream.

URLConnection getConnection()

用这个方法得到一个URLConnection连接后,就可以用URLConnection来获得相应协议的内容了,比如可以获得Http协议的头。 具体用法在以后的笔记中会提到。

public final Object getContent( ) throws IOException 这个方法的介绍看下面吧

The getContent( ) method is the third way to download data referenced by a URL. The getContent( ) method retrieves the data referenced by the URL and tries to make it into some type of object. If the URL refers to some kind of text object such as an ASCII or HTML file, the object returned is usually some sort of InputStream. If the URL refers to an image such as a GIF or a JPEG file, getContent( ) usually returns a java.awt.ImageProducer (more specifically, an instance of a class that implements the ImageProducer interface).

public final Object getContent(Class[] classes) throws IOException

返回与数组顺序对应的相应类型的类。

另外注意URLEncoder和 URLDecoder 类,它们是和字符编码相关的。

URI类的相关内容如果理解了“URI”的各个部分也就很好掌握了! 具体URI的各部分请看另一篇。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: