java.net.Url類的應用

來源:互聯網
上載者:User

認識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()">
  1. import java.io.*;        
  2. import java.net.URL;        
  3. import java.net.URLConnection;        
  4.       
  5. public class TestURL {        
  6.         public static void main(String[] args) throws IOException {        
  7.                 test4();        
  8.                 test3();        
  9.                 test2();        
  10.                 test();        
  11.         }        
  12.       
  13.         /**      
  14.          * 擷取URL指定的資源。      
  15.          *      
  16.          * @throws IOException      
  17.          */        
  18.         public static void test4() throws IOException {        
  19.                 URL url = new URL("http://lavasoft.blog.51cto.com/attachment/200811/200811271227767778082.jpg");        
  20.                 //獲得此 URL 的內容。        
  21.                 Object obj = url.getContent();        
  22.                 System.out.println(obj.getClass().getName());        
  23.         }        
  24.       
  25.         /**      
  26.          * 擷取URL指定的資源      
  27.          *      
  28.          * @throws IOException      
  29.          */        
  30.         public static void test3() throws IOException {        
  31.                 URL url = new URL("http://www.hrtsea.com/down/soft/45.htm");        
  32.                 //返回一個 URLConnection 對象,它表示到 URL 所引用的遠程對象的串連。        
  33.                 URLConnection uc = url.openConnection();        
  34.                 //開啟的串連讀取的輸入資料流。        
  35.                 InputStream in = uc.getInputStream();        
  36.                 int c;        
  37.                 while ((c = in.read()) != -1)        
  38.                         System.out.print(c);        
  39.                 in.close();        
  40.         }        
  41.       
  42.         /**      
  43.          * 讀取URL指定的網頁內容      
  44.          *      
  45.          * @throws IOException      
  46.          */        
  47.         public static void test2() throws IOException {        
  48.                 URL url = new URL("http://www.hrtsea.com/down/soft/45.htm");        
  49.                 //開啟到此 URL 的串連並返回一個用於從該串連讀入的 InputStream。        
  50.                 Reader reader = new InputStreamReader(new BufferedInputStream(url.openStream()));        
  51.                 int c;        
  52.                 while ((c = reader.read()) != -1) {        
  53.                         System.out.print((char) c);        
  54.                 }        
  55.                 reader.close();        
  56.         }        
  57.       
  58.         /**      
  59.          * 擷取URL的輸入資料流,並輸出      
  60.          *      
  61.          * @throws IOException      
  62.          */        
  63.         public static void test() throws IOException {        
  64.                 URL url = new URL("http://lavasoft.blog.51cto.com/62575/120430");        
  65.                 //開啟到此 URL 的串連並返回一個用於從該串連讀入的 InputStream。        
  66.                 InputStream in = url.openStream();        
  67.                 int c;        
  68.                 while ((c = in.read()) != -1)        
  69.                         System.out.print(c);        
  70.                 in.close();        
  71.         }        
  72. }       
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代碼
  1. import java.net.URL;        
  2.       
  3. public class MainClass {        
  4.       
  5.         public static void main(String[] args) {        
  6.       
  7.                 String host = "www.java2s.com";        
  8.                 String file = "/index.html";        
  9.       
  10.                 String[] schemes = {"http", "https", "ftp", "mailto", "telnet", "file", "ldap", "gopher",        
  11.                                 "jdbc", "rmi", "jndi", "jar", "doc", "netdoc", "nfs", "verbatim", "finger", "daytime",        
  12.                                 "systemresource"};        
  13.       
  14.                 for (int i = 0; i < schemes.length; i++) {        
  15.                         try {        
  16.                                 URL u = new URL(schemes, host, file);        
  17.                                 System.out.println(schemes + " is supported/r/n");        
  18.                         } catch (Exception ex) {        
  19.                                 System.out.println(schemes + " is not supported/r/n");        
  20.                         }        
  21.                 }        
  22.         }        
  23. }       
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.