List<String> res1 = new ArrayList<String>();
try {
// 根据网卡取本机配置的IP,适用于Windows和Linux
Enumeration netInterfaces = NetworkInterface.getNetworkInterfaces();
while (netInterfaces.hasMoreElements()) {
NetworkInterface ni = (NetworkInterface) netInterfaces.nextElement();
Enumeration<InetAddress> ids = ni.getInetAddresses();
while (ids.hasMoreElements()) {
InetAddress ip = ids.nextElement();
if(! (ip.isLoopbackAddress() || (ip.getHostAddress().indexOf(':') >= 0) ))
{
res1.add(ip.getHostAddress());
}
}
}
} catch (SocketException e) {
throw new RuntimeException(e);
}
for(String str : res1){
System.out.println(str);
}
try {
// 根据网卡取本机配置的IP,适用于Windows和Linux
Enumeration netInterfaces = NetworkInterface.getNetworkInterfaces();
while (netInterfaces.hasMoreElements()) {
NetworkInterface ni = (NetworkInterface) netInterfaces.nextElement();
Enumeration<InetAddress> ids = ni.getInetAddresses();
while (ids.hasMoreElements()) {
InetAddress ip = ids.nextElement();
if(! (ip.isLoopbackAddress() || (ip.getHostAddress().indexOf(':') >= 0) ))
{
res1.add(ip.getHostAddress());
}
}
}
} catch (SocketException e) {
throw new RuntimeException(e);
}
for(String str : res1){
System.out.println(str);
}