java获取应用服务器IP,使用Java获取服务器IP地址

使用Java获取服务器IP地址

public class NetworkInterfaceUtil {

private static List getInterfaces() throws SocketException {

return Collections.list(NetworkInterface.getNetworkInterfaces());

}

private static List getNonLoopBackInterfaces() throws SocketException {

return getInterfaces().stream()

.filter(i -> Unchecked.supplier(() -> !i.isLoopback()).get())

.collect(toList());

}

public static List getIp4Addresses() throws SocketException {

final List is = getNonLoopBackInterfaces();

return is.stream().flatMap(i -> {

final Enumeration addresses = i.getInetAddresses();

final Builder builder = Stream.builder();

while (addresses.hasMoreElements()) {

final InetAddress ip = addresses.nextElement();

if (!ip.isLoopbackAddress()) {

if (ip.getHostAddress().equalsIgnoreCase("127.0.0.1")) {

continue;

}

if (ip instanceof Inet6Address) {

continue;

}

if (ip instanceof Inet4Address) {

builder.add(ip.getHostAddress());

}

}

}

return builder.build();

}).collect(toList());

}

}

write on 2017-1-4

你可能感兴趣的:(java获取应用服务器IP)