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

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的各部分请看另一篇。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值