Java URLオブジェクトの理解と例


IPを認識し、URLを認識することは、ネットワークプログラミングの第一歩です.java.net.URLは豊富なURL構築方式を提供し、javaを通過することができる.net.URLでリソースを取得します.
一、認識URL
クラスURLは、インターネットの「リソース」へのポインタである統合リソースロケータを表します.リソースは、単純なファイルまたはディレクトリであってもよいし、データベースや検索エンジンのクエリーなど、より複雑なオブジェクトへの参照であってもよい.
簡単にURLは、プロトコル、ホスト名、ポート、パス、クエリー文字列、パラメータなどのオブジェクトが含まれていると理解できます.各セグメントは独立して設定できます.
アプリケーションは、別のURLに対するリソースに到達するのに十分な情報のみを含む「相対URL」を指定することもできます.HTMLページでは相対URLがよく用いられる.
相対的なURLは、URLのすべてのコンポーネントを指定する必要はありません.プロトコル、ホスト名、またはポート番号がない場合、これらの値は完全に指定されたURLから継承されます.
URLは、URLエスケープを理解していないため、同じURLのピア符号化および復号形式を識別することはできない.
なお、URIクラスは、特定の場合、その構成フィールドをエスケープする.URLの符号化と復号化をURIで管理し、toURI()とURI.toURL()を使用してこの2つのクラス間の変換を実現することを推奨します.
URLEncoderクラスとURLDecoderクラスを使用することもできますが、RFC 396で定義された符号化メカニズムとは異なるHTML形式の符号化にのみ適用されます.
二、URL指定のリソースを取得する
次に、指定したリソースを取得する方法を例に示します.

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();    
        }    
}

mport 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
"); } catch (Exception ex) { System.out.println(schemes + " is not supported\r
"); } } } }