脚本宝典收集整理的这篇文章主要介绍了使用Java获取服务器IP地址,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
public class NetworkInterfaceUtil { private static List<NetworkInterface> getInterfaces() throws SocketException { return Collections.list(NetworkInterface.getNetworkInterfaces()); } private static List<NetworkInterface> getNonLoopBackInterfaces() throws SocketException { return getInterfaces().stream() .filter(i -> Unchecked.supplier(() -> !i.isLoopback()).get()) .collect(toList()); } public static List<String> getIp4Addresses() throws SocketException { final List<NetworkInterface> is = getNonLoopBackInterfaces(); return is.stream().flatMap(i -> { final Enumeration<InetAddress> addresses = i.getInetAddresses(); final Builder<String> 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地址全部内容,希望文章能够帮你解决使用Java获取服务器IP地址所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。