認識IP、認識URL是進行網路編程的第一步。java.net.URL提供了豐富的URL構建方式,並可以通過java.net.URL來擷取資源。
一、認識URL
類 URL 代表一個統一資源定位器,它是指向互連網“資源”的指標。資源可以是簡單的檔案或目錄,也可以是對更為複雜的對象的引用,例如對資料庫或搜尋引擎的查詢。
簡單的可以把URL理解為包含:協議、主機名稱、連接埠、路徑、查詢字串和參數等對象。每一段可以獨立設定。
應用程式也可以指定一個“相對 URL”,它只包含到達相對於另一個 URL 的資源的足夠資訊。HTML 頁面中經常使用相對 URL.
相對 URL 不需要指定 URL 的所有組成部分。如果缺少協議、主機名稱或連接埠號碼,這些值將從完整指定的 URL 中繼承。
由於 URL 不懂 URL 轉義,所以它不會識別同一 URL 的對等編碼和解碼形式。
注意,URI 類在某些特定情況下對其組成欄位執行轉義。建議使用 URI 管理 URL 的編碼和解碼,並使用 toURI() 和 URI.toURL() 實現這兩個類之間的轉換。
也可以使用 URLEncoder 和 URLDecoder 類,但是只適用於 HTML 形式的編碼,它與 RFC2396 中定義的編碼機制不同。
(以上介紹來自Java API doc)
二、URL對象的構建
方式很多,可以看看API文檔。
三、擷取URL指定的資源
下面給個例子,說明如何擷取到指定的資源。
Java代碼 {
dp.sh.Toolbar.CopyToClipboard(this);return false;
}" href="#"> {
code_favorites_do_favorite(this);return false;
}" href="javascript:void()">
- import java.io.*;
- import java.net.URL;
- import java.net.URLConnection;
-
- public class TestURL {
- public static void main(String[] args) throws IOException {
- test4();
- test3();
- test2();
- test();
- }
-
- /**
- * 擷取URL指定的資源。
- *
- * @throws IOException
- */
- public static void test4() throws IOException {
- URL url = new URL("http://lavasoft.blog.51cto.com/attachment/200811/200811271227767778082.jpg");
- //獲得此 URL 的內容。
- Object obj = url.getContent();
- System.out.println(obj.getClass().getName());
- }
-
- /**
- * 擷取URL指定的資源
- *
- * @throws IOException
- */
- public static void test3() throws IOException {
- URL url = new URL("http://www.hrtsea.com/down/soft/45.htm");
- //返回一個 URLConnection 對象,它表示到 URL 所引用的遠程對象的串連。
- URLConnection uc = url.openConnection();
- //開啟的串連讀取的輸入資料流。
- InputStream in = uc.getInputStream();
- int c;
- while ((c = in.read()) != -1)
- System.out.print(c);
- in.close();
- }
-
- /**
- * 讀取URL指定的網頁內容
- *
- * @throws IOException
- */
- public static void test2() throws IOException {
- URL url = new URL("http://www.hrtsea.com/down/soft/45.htm");
- //開啟到此 URL 的串連並返回一個用於從該串連讀入的 InputStream。
- Reader reader = new InputStreamReader(new BufferedInputStream(url.openStream()));
- int c;
- while ((c = reader.read()) != -1) {
- System.out.print((char) c);
- }
- reader.close();
- }
-
- /**
- * 擷取URL的輸入資料流,並輸出
- *
- * @throws IOException
- */
- public static void test() throws IOException {
- URL url = new URL("http://lavasoft.blog.51cto.com/62575/120430");
- //開啟到此 URL 的串連並返回一個用於從該串連讀入的 InputStream。
- InputStream in = url.openStream();
- int c;
- while ((c = in.read()) != -1)
- System.out.print(c);
- in.close();
- }
- }
import java.io.*; import java.net.URL; import java.net.URLConnection; public class TestURL { public static void main(String[] args) throws IOException { test4(); test3(); test2(); test(); } /** * 擷取URL指定的資源。 * * @throws IOException */ public static void test4() throws IOException { URL url = new URL("http://lavasoft.blog.51cto.com/attachment/200811/200811271227767778082.jpg"); //獲得此 URL 的內容。 Object obj = url.getContent(); System.out.println(obj.getClass().getName()); } /** * 擷取URL指定的資源 * * @throws IOException */ public static void test3() throws IOException { URL url = new URL("http://www.hrtsea.com/down/soft/45.htm"); //返回一個 URLConnection 對象,它表示到 URL 所引用的遠程對象的串連。 URLConnection uc = url.openConnection(); //開啟的串連讀取的輸入資料流。 InputStream in = uc.getInputStream(); int c; while ((c = in.read()) != -1) System.out.print(c); in.close(); } /** * 讀取URL指定的網頁內容 * * @throws IOException */ public static void test2() throws IOException { URL url = new URL("http://www.hrtsea.com/down/soft/45.htm"); //開啟到此 URL 的串連並返回一個用於從該串連讀入的 InputStream。 Reader reader = new InputStreamReader(new BufferedInputStream(url.openStream())); int c; while ((c = reader.read()) != -1) { System.out.print((char) c); } reader.close(); } /** * 擷取URL的輸入資料流,並輸出 * * @throws IOException */ public static void test() throws IOException { URL url = new URL("http://lavasoft.blog.51cto.com/62575/120430"); //開啟到此 URL 的串連並返回一個用於從該串連讀入的 InputStream。 InputStream in = url.openStream(); int c; while ((c = in.read()) != -1) System.out.print(c); in.close(); } }
四、Java所支援的URL類型
Java代碼
- import java.net.URL;
-
- public class MainClass {
-
- public static void main(String[] args) {
-
- String host = "www.java2s.com";
- String file = "/index.html";
-
- String[] schemes = {"http", "https", "ftp", "mailto", "telnet", "file", "ldap", "gopher",
- "jdbc", "rmi", "jndi", "jar", "doc", "netdoc", "nfs", "verbatim", "finger", "daytime",
- "systemresource"};
-
- for (int i = 0; i < schemes.length; i++) {
- try {
- URL u = new URL(schemes, host, file);
- System.out.println(schemes + " is supported/r/n");
- } catch (Exception ex) {
- System.out.println(schemes + " is not supported/r/n");
- }
- }
- }
- }