Enumeration allNetInterfaces = NetworkInterface.getNetworkInterfaces();
InetAddress ip = null;
while (allNetInterfaces.hasMoreElements()) {
NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
System.out.println(netInterface.getName());
Enumeration aa = netInterface.getInetAddresses();
while (aa.hasMoreElements()) {
ip = (InetAddress) aa.nextElement();
if (ip != null && ip instanceof Inet4Address) {
System.out.println("本机的IP = " + ip.getHostAddress());
}
}
}
InetAddress ip = null;
while (allNetInterfaces.hasMoreElements()) {
NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
System.out.println(netInterface.getName());
Enumeration aa = netInterface.getInetAddresses();
while (aa.hasMoreElements()) {
ip = (InetAddress) aa.nextElement();
if (ip != null && ip instanceof Inet4Address) {
System.out.println("本机的IP = " + ip.getHostAddress());
}
}
}