net.luohuasheng.bee.rest.swagger.utils.LocalIpAddressUtils Maven / Gradle / Ivy
package net.luohuasheng.bee.rest.swagger.utils;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.*;
/**
* @author wusuoming
*/
public class LocalIpAddressUtils {
public static void main(String[] args) {
Map> map = getLocalIp();
for (String key : map.keySet()) {
System.out.println(key + ": " + map.get(key).toString());
}
System.out.println(getLocalIps());
}
public static List getLocalIps() {
Map> map = getLocalIp();
List ips = new ArrayList<>();
for (List value : map.values()) {
ips.addAll(value);
}
return ips;
}
public static Map> getLocalIp() {
Map> map = new HashMap<>(0);
Enumeration e1;
try {
e1 = NetworkInterface.getNetworkInterfaces();
while (e1.hasMoreElements()) {
NetworkInterface ni = e1.nextElement();
List ips = new LinkedList<>();
map.put(ni.getName(), ips);
Enumeration e2 = ni.getInetAddresses();
while (e2.hasMoreElements()) {
InetAddress ia = e2.nextElement();
if (ia instanceof Inet6Address) {
continue; // omit IPv6 address
}
ips.add(ia.getHostAddress());
}
}
} catch (SocketException e) {
e.printStackTrace();
}
return map;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy