基础知识

1.InetAddress类

在网络API套接字InetAddress类和它的子类型对象使用域名DNS系统,处理主机名到主机IPv4或IPv6地址的转换。如图1-1所示。

 Java学习---InetAddress类的学习

由于InetAddress类只有一个构造函数,且不能传递参数,所以不能直接创建该对象实例,比如下面的做法就是错误的:

InetAddress ia = new InetAddress ();             ×

  可通过以5个成员方法获得InetAddress对象或InetAddress数组:

l getAllByName(String host)方法返回一个InetAddress对象的引用,每个对象包含一个表示相应主机名的单独的IP地址,这个IP地址是通过host参数传递的,例如:

InetAddress [] ia = getAllByName(“MyHost”);

l getByAddress(byte [] addr)方法返回一个InetAddress对象的引用,这个对象包含了一个Ipv4地址或Ipv6地址,Ipv4地址是一个4字节数组,Ipv6地址是一个16字节地址数组。

l getByAddress(String host, byte [] addr)方法返回一个InetAddress对象的引用,这个InetAddress对象包含了一个由host和4字节的addr数组指定的IP地址,或者是host和16字节的addr数组指定的IP地址。

l getByName(String host)方法返回一个InetAddress对象,该对象包含了一个与host参数指定的主机相对应的IP地址。

l getLocalHost()方法返回一个InetAddress对象,这个对象包含了本地机的IP地址。

以上各方法均可能产生的UnknownHostException(未知的主机名)异常。当获得了InetAddress类对象的引用就可以调用InetAddress的各种方法来获得InetAddress子类对象中的IP地址信息。例,通过调用getCanonicalHostName()从域名服务中获得标准的主机名,getHostAddress()获得IP地址,getHostName()获得主机名,isLoopbackAddress()判断IP地址是否是一个loopback环回地址。

2.URL类

IP地址唯一标识了Internet上的计算机,而URL则标识了这些计算机上的资源。通常URL是一个包含了传输协议、主机名称、服务端口号、文件路径名称,以及间隔符号“://”“:”“/” 等信息的字符串,例:https://www.cnblogs.com/ftl1012/p/9346858.html

为了方便程序员编程,JDK中提供了URL类,该类的全名是java.net.URL,该类用于使用它的各种方法来对URL对象进行分割、合并等处理,如图1-2所示。

Java学习---InetAddress类的学习

 

URL有6种构造方法,通常使用了绝对URL路径构造方法,其中的URL参数是一个完整的URL字符串,而且必须要包含传输协议,如:

URL  raceHtml=new URL("https://www.cnblogs.com/ftl1012/p/9346858.html");

四、常用方法

1. InetAddress类主要方法

 byte[]

getAddress() 返回该对象的原始IP。

static InetAddress[]

getAllByName(String host) 获得指定主机域名的所有IP地址。

static InetAddress

getByAddress(byte[] addr) 获得指定IP地址对象。

static InetAddress

getByAddress(String host, byte[] addr) 根据指定主机名和IP地址创建对象。

static InetAddress

getByName(String host) 根据指定主机名获得对象。

 String

getCanonicalHostName() 获得指定域名的法定信息。

 String

getHostAddress() 返回当前IP地址字符串。

 String

getHostName() 获得当前IP地址的主机名。

static InetAddress

getLocalHost() 获得本地主机。

 boolean

isLoopbackAddress() 判断IP是否为环回地址。

2. URL类主要方法

 String

getAuthority() 获得URL的认证部分。

 Object

getContent() 获得URL的内容。

 Object

getContent(Class[] classes) 获得URL的内容

 int

getDefaultPort()获得URL默认的端口号

 String

getFile() 获得URL的文件名

 String

getHost()获得该URL的主机名

 String

getPath() 获得该URL的路径

 int

getPort() 获得该URL的端口

 String

getProtocol()获得该URL的协议。

 String

getQuery()获得该URL的查询部分

 String

getRef()获得该URL的锚部分

 String

getUserInfo()获得该URL的用户信息

 URLConnection

openConnection() 返回URL的连接。

 InputStream

openStream()打开URL的输入读取流

代码示例

根据主机名查找IP

 1 import java.net.InetAddress;
 2 import java.util.*;
 3 
 4 public class My
 5 {
 6     public static void main(String[] args)
 7     {
 8         String local = null ; 
 9         for(int i =18;i<20;i++){
10         local = "FF109Lx-"+i ;
11         try{
12             InetAddress inet = InetAddress.getByName(local) ;
13             byte[] ip = inet.getAddress();
14             System.out.print("FF109Lx-"+i);
15             for(int x=0;x<ip.length;x++){
16                 int ips = (ip[x]>=0?ip[x]:(ip[x]+256)) ;
17                 System.out.print(" "+ips+"." ) ;
18             }
19         }catch(Exception e ){
20             System.err.println("FF109Lx-"+i+" 获得本地信息失败..." );
21             System.out.println();
22         }
23         }
24     }
25 }
View Code

相关文章: