public static String getCurrentIp() { try { Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces(); while (networkInterfaces.hasMoreElements()) { NetworkInterface ni = (NetworkInterface) networkInterfaces.nextElement(); Enumeration<InetAddress> nias = ni.getInetAddresses(); while (nias.hasMoreElements()) { InetAddress ia = (InetAddress) nias.nextElement(); if (!ia.isLinkLocalAddress() && !ia.isLoopbackAddress() && ia instanceof Inet4Address) { return ia.toString(); } } } } catch (SocketException e) { System.out.println("Fail to get currentIp."); } return null; }
当前IP
原文地址:https://www.cnblogs.com/codegod/p/8488350.html
时间: 2024-10-11 16:30:13