JAVA學習筆記(五十八)- InetAddress類與URL

來源:互聯網
上載者:User

標籤:inetaddres   url   執行個體   

InetAddress類
/* * InetAddress類  */public class Test07 {    public static void main(String[] args) throws UnknownHostException {        //擷取原生InetAddress執行個體         System.out.println("*******擷取原生InetAddress執行個體**********");        InetAddress address1 = InetAddress.getLocalHost();        System.out.println("主機名稱:"+address1.getHostName());        System.out.println("IP地址:"+address1.getHostAddress());        byte[] bytes=address1.getAddress();        System.out.println("位元組數組形式的IP地址:"+Arrays.toString(bytes));//超過127則為負數,加256        System.out.println(address1);        //根據主機名稱擷取指定主機的InetAddress執行個體        System.out.println("*******根據主機名稱擷取指定主機的InetAddress執行個體**********");        //InetAddress address2=InetAddress.getByName("HE67B6DJBOID2DA");        InetAddress address2=InetAddress.getByName("5.8.6.45");        System.out.println(address2.getHostName());        System.out.println(address2);        //根據位元組數組形式的IP地址來擷取InetAddress執行個體        System.out.println("**********根據位元組數組形式的IP地址來擷取InetAddress執行個體***********");        byte[] bytes2={(byte)192,(byte)168,6,8};        InetAddress address3=InetAddress.getByAddress(bytes2);        System.out.println(address3.getHostName());        System.out.println(address3);    }}
URL常用方法
/* * URL常用方法 */public class Test08 {    public static void main(String[] args) throws MalformedURLException {        URL baidu=new URL("http://www.baidu.com");        URL url=new URL(baidu, "/index.html?username=tom&password=123#test");        System.out.println("協議:"+url.getProtocol());        System.out.println("主機:"+url.getHost());        System.out.println("連接埠:"+url.getPort());        System.out.println("檔案路徑:"+url.getPath());        System.out.println("檔案名稱:"+url.getFile());        System.out.println("相對路徑:"+url.getRef());        System.out.println("查詢:"+url.getQuery());    }}
使用URL讀取網頁內容
import java.io.BufferedInputStream;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.net.MalformedURLException;import java.net.URL;/* * 使用URL讀取網頁內容 */public class Test01 {    public static void main(String[] args) throws IOException {        test02();    }    //讀取網頁資源    public static void test01() throws IOException{        URL url = new URL("http://localhost:8080/webTest/");        InputStream is = url.openStream();// 擷取URL對象所表示的資源的輸入資料流        BufferedReader br = new BufferedReader(new InputStreamReader(is));// 將位元組輸入資料流轉換為字元輸入資料流        String str = br.readLine();        while (str != null) {            System.out.println(str);            str = br.readLine();        }        br.close();        is.close();    }    //讀取FTP上的檔案資源    public static void test02() throws IOException{        URL url=new URL("ftp://wbs14061:[email protected]/資料/exe2.txt");        System.out.println("協議:"+url.getProtocol());        System.out.println("主機:"+url.getHost());        System.out.println("連接埠:"+url.getPort());        System.out.println("檔案路徑:"+url.getPath());        System.out.println("檔案名稱:"+url.getFile());        System.out.println("相對路徑:"+url.getRef());        System.out.println("查詢:"+url.getQuery());        InputStream is = url.openStream();// 擷取URL對象所表示的資源的輸入資料流        BufferedReader br = new BufferedReader(new InputStreamReader(is));// 將位元組輸入資料流轉換為字元輸入資料流        String str = br.readLine();        while (str != null) {            System.out.println(str);            str = br.readLine();        }        br.close();        is.close();    }}

JAVA學習筆記(五十八)- InetAddress類與URL

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.