content: package cn.itbat.microsoft.utils;
  
  import java.net.Inet4Address;
  import java.net.InetAddress;
  import java.net.NetworkInterface;
  import java.net.SocketException;
  import java.util.ArrayList;
  import java.util.Collections;
  import java.util.Enumeration;
  
  /**
   * @author huahui.wu
   * @date 2020年10月28日 17:12:27
   */
  public class IpUtils {
      public static void main(String[] args) {
          System.out.println( getLocalIpAddr());
      }
  
      public static ArrayList<String> getLocalIpAddr() {
          ArrayList<String> ipList = new ArrayList<String>();
          InetAddress[] addrList;
          try {
              Enumeration interfaces = NetworkInterface.getNetworkInterfaces();
              while (interfaces.hasMoreElements()) {
                  NetworkInterface ni = (NetworkInterface) interfaces.nextElement();
                  Enumeration ipAddrEnum = ni.getInetAddresses();
                  while (ipAddrEnum.hasMoreElements()) {
                      InetAddress addr = (InetAddress) ipAddrEnum.nextElement();
                      if (addr.isLoopbackAddress() == true) {
                          continue;
                      }
  
                      String ip = addr.getHostAddress();
                      if (ip.indexOf(":") != -1) {
                          //skip the IPv6 addr
                          continue;
                      }
                      ipList.add(ip);
                  }
              }
  
              Collections.sort(ipList);
          } catch (Exception e) {
              e.printStackTrace();
          }
  
          return ipList;
      }
      /**
       * 获取本地真正的IP地址，即获得有线或者 无线WiFi 地址。
       * 过滤虚拟机、蓝牙等地址
       *
       * @return IPv4
       */
      public static String getRealIP() {
          try {
              Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces();
              while (allNetInterfaces.hasMoreElements()) {
                  NetworkInterface netInterface = allNetInterfaces.nextElement();
                  // 去除回环接口，子接口，未运行和接口
                  if (netInterface.isLoopback() || netInterface.isVirtual() || !netInterface.isUp()) {
                      continue;
                  }
  
                  if (!netInterface.getDisplayName().contains("Intel")
                          && !netInterface.getDisplayName().contains("Realtek")
                          && !netInterface.getDisplayName().contains("Ethernet")) {
                      continue;
                  }
  
                  Enumeration<InetAddress> addresses = netInterface.getInetAddresses();
                  while (addresses.hasMoreElements()) {
                      InetAddress ip = addresses.nextElement();
                      if (ip != null) {
                          // ipv4
                          if (ip instanceof Inet4Address) {
                              return ip.getHostAddress();
                          }
                      }
                  }
                  break;
              }
          } catch (SocketException e) {
              //捕获异常
          }
          return null;
      }
  
  }