import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;
//获取Ip地址
public class GetIP {
public static void main(String[] args) {
try {
//获取程序所在主机的主机名以及Ip地址
InetAddress address =InetAddress.getLocalHost();
System.out.println("主机:"+address);
System.out.println("主机名:"+address.getHostName()+"\n"+"IP:"+address.getHostAddress());
//获取本机回路地址
address=InetAddress.getByName(null);
System.out.println("主机:"+address);
System.out.println("主机名:"+address.getHostName()+"\n"+"IP:"+address.getHostAddress());
//获取新浪web主机Ip地址
InetAddress[] address1=InetAddress.getAllByName("www.sina.com.cn"); //防止多个Ip地址
System.out.println("新浪Ip:"+Arrays.toString(address1));
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
java Ip地址的获取 简单代码
最新推荐文章于 2021-02-26 20:44:54 发布