try { Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { NetworkInterface iface = interfaces.nextElement(); if (iface.isLoopback() || !iface.isUp()) { continue; } Enumeration<InetAddress> addresses = iface.getInetAddresses(); while (addresses.hasMoreElements()) { InetAddress addr = addresses.nextElement(); if (addr instanceof Inet6Address) { continue; } ip = addr.getHostAddress(); } } } catch (SocketException e) { LoggerUtils.error(String.format("获取本机IP异常:%s", e.getStackTrace()), null); ip = "127.0.0.1"; }
原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/15977.html