java真正获取ip

try {
            for (Enumeration interfaces = NetworkInterface.getNetworkInterfaces(); interfaces.hasMoreElements();) {
                NetworkInterface networkInterface = interfaces.nextElement();
                if (networkInterface.isLoopback() || networkInterface.isVirtual() || !networkInterface.isUp()) {
                    continue;
                }
                List addresses = networkInterface.getInterfaceAddresses();
                for (InterfaceAddress interfaceAddress : addresses) {
                    String ip = interfaceAddress.getAddress().getHostAddress();
                    if (ip.length() < 20) {
                        System.out.println("IP:" + ip);
                    }
                }
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }

 

你可能感兴趣的:(java笔记)