1. 程式人生 > >Java 獲取當前環境Windows/Linux 真實IP

Java 獲取當前環境Windows/Linux 真實IP

system class AD span gpo etc ack img network

技術分享圖片
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

Java 獲取當前環境Windows/Linux 真實IP