InetAddress類和InetSocketAddress類


InetAddress 類

封裝計算機的 IP 地址,不包含端口號

InetAddress 類常用的方法

1 String getHostAddress() 獲得 IP 地址

2 String getHostName() 獲得主機名

3 static InetAddress getByName(String host) 根據主機名獲得 IP 地址

 1 import java.net.InetAddress;
 2 import java.net.UnknownHostException;
 3 
 4 public class TestInetAddress {
 5 
 6     public static void main(String[] args) throws UnknownHostException {
 7         
 8         InetAddress localHost = InetAddress.getLocalHost(); //本機
 9         System.out.println("本機IP地址:" + localHost.getHostAddress());
10         System.out.println("本機名稱:" + localHost.getHostName());
11         
12         //根據域名得到InetAddress對象
13         InetAddress bd = InetAddress.getByName("www.baidu.com");
14         System.out.println("百度服務器地址:" + bd.getHostAddress());
15         System.out.println("百度服務器名稱:" + bd.getHostName());
16         
17         //根據IP地址得到InetAddress對象
18         InetAddress ia = InetAddress.getByName("39.130.131.42");
19         System.out.println("服務器主機IP:" + ia.getHostAddress());
20         //如果39.130.131.42IP地址不存在或者DNS(域名解析系統)不允許進行IP地址和域名的映射,就會直接返回域名地址
21         System.out.println("主機名稱" + ia.getHostName());
22     }
23 
24 }

運行結果:

-------------------------------------------------------------------------

 

InetSocketAddress 類

此類用於實現 IP 套接字地址 (IP 地址+端口號),用於socket 通信

InetSocketAddress 類常用的方法

1 InetAddress getAddress() 獲取 InetAddress 對象

2 int getPort() 獲取端口號

3 String getHostName() 獲取主機名

 1 import java.net.InetAddress;
 2 import java.net.InetSocketAddress;
 3 import java.net.UnknownHostException;
 4 
 5 public class TestInetSocketAddress {
 6 
 7     public static void main(String[] args) throws UnknownHostException {
 8         
 9         //創建對象
10         InetSocketAddress is1 = new InetSocketAddress("localhost", 9999);
11         InetSocketAddress is2 = new InetSocketAddress("127.0.0.1", 9999);
12         InetSocketAddress is3 = new InetSocketAddress("192.168.136.1", 9999);
13         
14         InetAddress ia = InetAddress.getByName("192.168.136.1");
15         InetSocketAddress is4 = new InetSocketAddress(ia, 9999);
16         System.out.println("主機名稱:" + is4.getHostName());
17         System.out.println("主機IP地址:" + is4.getAddress());
18     }
19 
20 }

-----------------------------------------------------------------------------------------------------------------------------

 

URL類

URL(Uniform Resource Locator)統一資源定位符,由 4 部分組成:協議 、存放資源的主機域名、端口號和資源文件名。

URL 是指向互聯網“資源”的指針資源可以是簡單的文件或目錄,也可以是對更為復雜的對象的引用,例如對數據庫或搜索引擎的查詢

URL 類常用方法

1 String getProtocal() 獲取此 URL 的協議名稱

2 String getHost() 獲取此 URL 的主機名(如果適用)

3 int getPort() 獲取 URL 的端口號

4 String getFile() 獲取此 URL 的文件名

5 getDefaultPort() 獲取與此 URL 關聯協議的默認端口號

6 getPath() 獲取此 URL 的路徑部分

 1 import java.net.MalformedURLException;
 2 import java.net.URI;
 3 import java.net.URL;
 4 
 5 public class TestUrl {
 6     public static void main(String[] args) throws MalformedURLException {
 7         
 8         URL url = new URL("https://www.baidu.com:80/index.html");
 9         System.out.println("協議名稱:" + url.getProtocol());
10         System.out.println("主機名稱:" + url.getHost());
11         System.out.println("端口號:" + url.getPort()); //URL不指明端口號則getPort()返回-1
12         System.out.println("獲取資源路徑:" + url.getFile());
13         System.out.println("獲取資源路徑:" + url.getPath());
14         System.out.println("獲取默認端口:" + url.getDefaultPort());
15     }
16     
17 }

-------------------------------------------------------------------------------------------------------

openStream()方法   打開到此 URL 的連接並返回一個用於從該連接讀入的InputStream

 1 import java.io.BufferedReader;
 2 import java.io.BufferedWriter;
 3 import java.io.FileOutputStream;
 4 import java.io.IOException;
 5 import java.io.InputStream;
 6 import java.io.InputStreamReader;
 7 import java.io.OutputStreamWriter;
 8 import java.net.MalformedURLException;
 9 import java.net.URL;
10 
11 public class TestUrl2 {
12     public static void main(String[] args) throws IOException {
13         /**網絡爬蟲
14          * (1)從網絡上獲取資源
15          * (2)存儲到本機
16          */
17         //(1)創建URL對象
18         URL url = new URL("https://www.baidu.com");  //獲取主頁資源
19         //(2)獲取字節輸入流
20         InputStream is = url.openStream();
21         //(3)緩沖流
22         BufferedReader br = new BufferedReader(new InputStreamReader(is, "utf-8"));
23         //(4)存儲到本地
24         BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("F://index.html"), "utf-8"));
25         //(5)邊讀邊寫
26         String line = null;
27         while( (line = br.readLine()) != null) {
28             bw.write(line); //寫入
29             bw.newLine();  //換行
30             bw.flush();  //清空緩沖區
31         }
32         //(6)關閉流
33         bw.close();
34         br.close();
35     }
36 
37 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM