获取 ip 地址

  1. StringBuilder IFCONFIG=new StringBuilder();  
  2.    try {  
  3.        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {  
  4.            NetworkInterface intf = en.nextElement();  
  5.            for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {  
  6.                InetAddress inetAddress = enumIpAddr.nextElement();  
  7.                if (!inetAddress.isLoopbackAddress() && !inetAddress.isLinkLocalAddress() && inetAddress.isSiteLocalAddress()) {  
  8.                IFCONFIG.append(inetAddress.getHostAddress().toString()+"\n");  
  9.                }  
  10.   
  11.            }  
  12.        }  
  13.    } catch (SocketException ex) {  
  14.    }  
  15.    System.out.println(IFCONFIG);
posted @ 2017-12-06 01:35  aLa神灯  阅读(129)  评论(0编辑  收藏  举报