1.怎么获取ip相关信息
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration; public class Test { public static void main(String[] args) throws Exception{ java.net.InetAddress address=InetAddress.getByName("www.baidu.com");
System.out.println(address.getHostName());
System.out.println(address.getHostAddress()); InetAddress[] addresss=InetAddress.getAllByName("www.baidu.com");
for(InetAddress a : addresss){
System.out.println(a);
} InetAddress localAddress=InetAddress.getLocalHost();
System.out.println(localAddress); //判断IP地址
byte[] addressByte=localAddress.getAddress();
if(addressByte.length==4){
System.out.println("这个是ip4地址!");
}else if(addressByte.length==16){
System.out.println("这个是ip6地址!");
}else{
System.out.println("未知地址...");
} NetworkInterface netInterface=NetworkInterface.getByInetAddress(localAddress);
System.out.println(netInterface); //获取所有的网络接口
Enumeration<NetworkInterface> netInNetworkInterface=NetworkInterface.getNetworkInterfaces();
while(netInNetworkInterface.hasMoreElements()){
NetworkInterface ni=netInNetworkInterface.nextElement();
System.out.println(ni);
}
} }