Java 获取当前环境Windows/Linux 真实IP

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

 

posted @ 2018-03-01 14:45  川雨淅  阅读(181)  评论(0编辑  收藏  举报