23.07.2013 Views

Java IO.pdf - Nguyen Dang Binh

Java IO.pdf - Nguyen Dang Binh

Java IO.pdf - Nguyen Dang Binh

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Java</strong> I/O<br />

In particular, URL connections provide not just input streams by which the client can read<br />

data from the server, but also output streams to send data from the client to the server. This is<br />

essential for protocols like mailto.<br />

The java.net.URLConnection class is an abstract class that handles communication with<br />

different kinds of servers, like FTP servers and web servers. Protocol-specific subclasses of<br />

URLConnection, hidden inside the sun classes, handle different kinds of servers.<br />

5.2.1 Reading Data from URL Connections<br />

URL connections take place in five steps:<br />

1. The URL object is constructed.<br />

2. The openConnection() method of the URL object creates the URLConnection object.<br />

3. The parameters for the connection and the request properties that the client sends to<br />

the server are set up.<br />

4. The connect() method makes the connection to the server, perhaps using a socket for<br />

a network connection or a file input stream for a local connection. The response<br />

header information is read from the server.<br />

5. Data is read from the connection by using the input stream returned by<br />

getInputStream() or through a content handler with getContent(). Data can be<br />

sent to the server using the output stream provided by getOutputStream().<br />

This scheme is very much based on the HTTP/1.0 protocol. It does not fit other schemes that<br />

have a more interactive "request, response, request, response, request, response" pattern<br />

instead of HTTP/1.0's "single request, single response, close connection" pattern. In<br />

particular, FTP and even HTTP/1.1 aren't well suited to this pattern. I wouldn't be surprised to<br />

see this replaced with something more general in a future version of <strong>Java</strong>.<br />

URLConnection objects are not constructed directly in your own programs. Instead, you<br />

create a URL for the particular resource, and call that URL's openConnection() method. This<br />

gives you a URLConnection. Then the getInputStream() method returns an input stream<br />

that reads data from the URL. (The openStream() method of the URL class is just a thin<br />

veneer over the getInputStream() method of the URLConnection class.) For example:<br />

try {<br />

URL u = new URL("http://www.digitalthink.com/");<br />

URLConnection uc = u.openConnection();<br />

uc.connect();<br />

InputStream in = uc.getInputStream();<br />

//...<br />

}<br />

catch (<strong>IO</strong>Exception e) { //...<br />

If the connection cannot be opened (possibly because the remote host is unreachable), an<br />

<strong>IO</strong>Exception is thrown. Example 5.2 reads a URL from the command line, opens a<br />

connection to that URL, then prints the data returned by the server at that URL. This is similar<br />

to Example 5.1; WebCat differs primarily in that it uses a URLConnection instead of a URL.<br />

63

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!