創建和使用URL訪問網絡上的資源
URL(Uniform Resource Locator)是統一資源定位符的簡稱,它表示Internet上某一資源的地址。
通過URL我們可以訪問Internet上的各種網絡資源,比如最常見的WWW, FTP站點。瀏覽器通過解析給定的URL可以在網絡上查找相應的文件或其他資源。
在目前使用最為廣泛的TCP/IP中對於URL中主機名的解析也是協議的一個標准,即所謂的域名解析服務。
使用URL進行網絡編程,不需要對協議本身有太多的了解,功能也比較弱,相對而言是比較簡單的。
URL組成
一個URL包括兩個主要部分:
協議標識符:HTTP, FTP, File等。
資源名字:主機名,文件名,端口號,引用。
創建URL
在Java程序中,可以創建表示URL地址的URL對象。
URL對象表示一個絕對的URL地址,但URL對象可用絕對URL、相對URL和部分URL構建。
創建URL的代碼如下,如果創建失敗會拋出異常:
try { URL myURL = new URL("http://www.google.com.tw/"); } catch (MalformedURLException e) { //exception handler code here }
獲得URL對象的各個屬性
URL類中有各種用於獲取屬性的方法:
getProtocol
getHost
getPort
getFile
getRef
例子程序如下:

package com.example.network; import java.net.MalformedURLException; import java.net.URL; public class URLTest01 { public static void main(String[] args) { try { URL myURL = new URL( "http://java.sun.com:80/docs/books/tutorial/index.html#DOWN"); String protocal = myURL.getProtocol(); String host = myURL.getHost(); String file = myURL.getFile(); int port = myURL.getPort(); String ref = myURL.getRef(); System.out.println(protocal + ", " + host + ", " + file + ", " + port + ", " + ref); } catch (MalformedURLException e) { // exception handler code here } } }
創建和使用URL訪問網上資源
為獲得URL的實際比特或內容信息,用它的openConnection()方法從它創建一個URLConnection對象,與調用URL對象相關,它返回一個URLConnection對象。它可能引發IOException異常。
URLConnection是訪問遠程資源屬性的一般用途的類。如果你建立了與遠程服務器之間的連接,你可以在傳輸它到本地之前用URLConnection來檢查遠程對象的屬性。這些屬性由HTTP協議規范定義並且僅對用HTTP協議的URL對象有意義。
URL和URLConnection類對於希望建立與HTTP服務器的連接來獲取信息的簡單程序來說是非常好的。
例子程序UrlConnection01,建立連接,從連接對象獲取輸入流,然后讀入,再寫出到文件中去。

package com.example.network; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; public class UrlConnection01 { public static void main(String[] args) throws Exception { URL url = new URL("http://www.baidu.com/"); // 打開連接 URLConnection conn = url.openConnection(); // 得到輸入流 InputStream is = conn.getInputStream(); // 關於IO流的用法和寫法一定要熟悉 OutputStream os = new FileOutputStream("d:\\baidu.txt"); byte[] buffer = new byte[2048]; int length = 0; while (-1 != (length = is.read(buffer, 0, buffer.length))) { os.write(buffer, 0, length); } is.close(); os.close(); } }
也可以直接從URL對象獲取輸入流,見例子程序UrlConnection02。

package com.example.network; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; public class UrlConnection02 { public static void main(String[] args) throws Exception { URL url = new URL("http://www.baidu.com/"); // // 打開連接 // URLConnection conn = url.openConnection(); // // // 得到輸入流 // InputStream is = conn.getInputStream(); // 另一種得到輸入流的方法:通過url直接獲取 InputStream is = url.openStream(); // 關於IO流的用法和寫法一定要熟悉 OutputStream os = new FileOutputStream("d:\\baidu.txt"); byte[] buffer = new byte[2048]; int length = 0; while (-1 != (length = is.read(buffer, 0, buffer.length))) { os.write(buffer, 0, length); } is.close(); os.close(); } }
查看源代碼可以看到內部實現機制是一樣的:
public final InputStream openStream() throws java.io.IOException
{ return openConnection().getInputStream(); }
程序代碼UrlConnection03用字符流的方式讀取網站內容顯示在控制台上。

package com.example.network; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.URL; public class UrlConnection03 { public static void main(String[] args) throws Exception { URL url = new URL("http://www.google.com.tw/"); BufferedReader br = new BufferedReader(new InputStreamReader( url.openStream())); String line = null; while (null != (line = br.readLine())) { System.out.println(line); } br.close(); } }
參考資料
聖思園張龍老師Java SE系列視頻教程。