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 }

 

posted @ 2019-04-29 10:13  北风吹沙  阅读(4996)  评论(0编辑  收藏  举报