获取真实IP地址

 public static String getLocalIP() {       
         try{
     Enumeration<?> allNetInterfaces = NetworkInterface.getNetworkInterfaces();
     InetAddress ine = null;
     String ip="";
     while (allNetInterfaces.hasMoreElements())
     {
     NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
    // System.out.println(netInterface.getName());
     Enumeration<?> addresses = netInterface.getInetAddresses();
     while (addresses.hasMoreElements())
     {
      ine = (InetAddress) addresses.nextElement();
     if (ine != null && ine instanceof Inet4Address)
     {
      if(!ine.getHostAddress().equals("127.0.0.1")){
      ip=ine.getHostAddress();
      }
     //System.out.println("本机的IP = " + ine.getHostAddress());
     }
     }
     }
    return ip;
    } catch (Exception e) {
              e.printStackTrace();
              return "";
          }
     }

你可能感兴趣的:(IP地址)