Java Network Programming 笔记(3)

笔记

 
Java Network Programming 笔记

n5

三 利用URL类获取数据
chapter 7 Retrieving Data with URLs

1 建立URL对象
当JVM不支持url的协议时抛出MalformedURLException

(1)public URL(String url) throws MalformedURLException

(2)public URL(String protocol, String hostname, String file) throws MalformedURLException
此构造器设置port为-1,所以协议的默认端口将被使用。
file参数应该以"/"开始,包含一个路径,文件名,和一个可选的锚点
例如: URL u = new URL("http","www.eff.org","/blueribbon.html#intro") ;

(3)public URL(String protocol,String host, int port, String file) throws MalformedURLException
可以指定端口,其他和第二个构造器相同

(4)public URL(URL base,String relative) throws MalformedURLException
从一个相对的URL地址和一个base URL对象创建一个绝对URL对象
例如:
try{
 URL u1 = new URL("http://www.myweb.com/java/index.html") ;
 URL u2 = new URL(u1,"test.html") ;
}
catch(MalformedURLException e){}
去掉u1的文件名,然后加上新文件名test.html,就构成了u2

2得到URL的组成
getFile() 返回总的路径(并非文件名)即为URL中从第一个"/"开始到"#"为止的内容。如果没有file部分,java1.3返回一个空字符串,java1.1,1.2返回"/"
getHost() 返回主机名,不包含user:user这样的用户信息
getPort() 如果URL中没指定端口,则返回-1
getProtocol()
getRef() 返回指定的锚点,如果没有锚点返回null

//since java1.3
getQuery()
getPath() 和getFile()完全相同
getUserInfo()
getAuthority()

3 从URL得到数据
(1) public final InputStream openStream() throws IOException
打开URL,得到一个InputStream得到数据
(2) public URLConnection openConnection() throws IOException
打开URL,得到一个URLConnection对象,URLConnection对象表示网络资源的一个开放连接。当想要直接与服务器通讯时可使用该方法。URLConnection可以得到服务器发送的任何东西,而不仅仅是文档本身。而且还可以写数据。
(3) public final Object getContent() throws IOException
将从URL下载来的数据看作一个对象,比如图片,文本,声音,或一个InputStream对于不能理解的对象。可以用instance of判断到底是哪种对象,并cast为该对象。
(4) public final Object getContent(Class[] classes) throws IOException //java1.3
可指定哪些种类的class被返回。该方法按照classes数组的顺序依次尝试返回内容。

4 工具方法
public boolean sameFile(URL other)
测试两个URL是否指向同一个文件。
public String toExternalForm()
返回一个人类可读的String表示URL,和toString()方法等同。因此不常用这个方法。

5 public boolean equals(Object o)
相等的含义:Object o也是一个URL对象,两个URL都指向同一个file(sameFile()定义的),并且两个URL有相同的引用或都是null。

时间: 2024-10-30 21:46:59

Java Network Programming 笔记(3)的相关文章

Java Network Programming 笔记(1)

笔记   Java Network Programming 笔记 n5 一 网络基本概念Chapter2 Basic Network Concepts 2.1 Networkskeywords: network, node, host, address, name, packet-switched, protocol 网络是可以或多或少实时地相互发送和接收数据的计算机和其他设备的集合. 网络上的每台机器被称作结点(node),大多数结点是计算机,但是打印机,路由器,桥,网关,哑终端和可口可乐机都

Java Network Programming 笔记(2)

笔记   Java Network Programming 笔记 n5 二 查找Internet地址Chapter 6 Looking up Internet Addresses java.net.InetAddress类是java对IP地址的封装,这个类被其他大多数网络类使用,包括Socket,ServerSocket,URL,DatagramSocket,DatagramPacket.该类中包含hostName和address,但不是public的. 1 获得InetAddress对象Ine

Java Thread Programming 1.8.4 - Inter-thread Communication

Streaming Data Between Threads Using Pipes The java.io package provides many classes for writing and reading data to and from streams. Most of the time, the data is written to or read from a file or network connection. Instead of streaming data to a

Java Thread Programming 1.8.2 - Inter-thread Communication

  Missed Notification A missed notification occurs when threadB tries to notify threadA, but threadA is not yet waiting for the notification. In a multithreaded environment like Java, you don't have much control over which thread runs and for how lon

Java Thread Programming 1.8.3 - Inter-thread Communication

CubbyHole Example The class CubbyHole (see Listing 8.9) simulates a cubbyhole. A cubbyhole is a slot that can have only one item in it at a time. One thread puts an item into the slot and another thread takes it out. If a thread tries to put an item

Java Thread Programming 1.8.2 - Inter-thread Commu

Missed NotificationA missed notification occurs when threadB tries to notify threadA, but threadA is not yet waiting for the notification. In a multithreaded environment like Java, you don't have much control over which thread runs and for how long.

Java Thread Programming 1.8.3 - Inter-thread Commu

CubbyHole ExampleThe class CubbyHole (see Listing 8.9) simulates a cubbyhole. A cubbyhole is a slot that can have only one item in it at a time. One thread puts an item into the slot and another thread takes it out. If a thread tries to put an item i

深入Java虚拟机读书笔记[1:4]

第一章 Java体系结构 1. Java体系结构 the Java programming language the Java class file format the Java Application Programming Interface the Java Virtual Machine 2. Java语言优点或使用的技术: object-orientation multi-threading structured error-handling garbage collection d

Network Programming Using Libevent - (III)

這次要談的跟 Network Programming 沒有直接的關係. 在寫 Nonblocking Network Program 通常要處理 Buffering 的問題,但並不好寫,主要是因為 read() 或 recv() 不保證可以一次讀到一行的份量進來. 在 libevent 裡面提供相當不錯的 Buffer Library 可以用,完整的說明在 man event 的時候可以看到,最常用的應該就是以 evbuffer_add().evbuffer_readline() 這兩個 Fun