All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.bestwu.lang.util.IPAddressUtil Maven / Gradle / Ivy

There is a newer version: 1.1.6
Show newest version
package cn.bestwu.lang.util;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import javax.servlet.http.HttpServletRequest;
import org.springframework.util.StringUtils;

/**
 * IP工具
 *
 * @author Peter Wu
 */
public class IPAddressUtil {

  /**
   * 获取本机IP
   * @return ip
   */
  public static String getInet4Address() {
    try {
      Enumeration interfaces = NetworkInterface.getNetworkInterfaces();
      while (interfaces.hasMoreElements()) {
        NetworkInterface iface = interfaces.nextElement();
        // filters out 127.0.0.1 and inactive interfaces
        if (iface.isLoopback() || !iface.isUp()) {
          continue;
        }

        Enumeration addresses = iface.getInetAddresses();
        while (addresses.hasMoreElements()) {
          InetAddress addr = addresses.nextElement();
          if (addr instanceof Inet4Address) {
            return addr.getHostAddress();
          }
        }
      }
    } catch (SocketException e) {
      throw new RuntimeException(e);
    }

    return "127.0.0.1";
  }

  /**
   * 获取客户端IP
   *
   * @param request http请求
   * @return ip
   */
  public static String getClientIp(HttpServletRequest request) {

    String ip = request.getHeader("X-Forwarded-For");
    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
      ip = request.getHeader("Proxy-Client-IP");
    }
    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
      ip = request.getHeader("WL-Proxy-Client-IP");
    }
    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
      ip = request.getHeader("HTTP_CLIENT_IP");
    }
    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
      ip = request.getHeader("HTTP_X_FORWARDED_FOR");
    }
    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
      ip = request.getRemoteAddr();
    }
    if (ip == null) {
      ip = "unknown";
    }
    return ip;
  }

  /**
   * 是否为外网
   *
   * @param ipAddress ip
   * @return 是否为外网
   */
  public static boolean isExtranet(String ipAddress) {
    if (!StringUtils.hasText(ipAddress)) {
      throw new IllegalArgumentException("ipAddress 不能为空");
    }
    return !ipAddress.matches(
        "(127\\.0\\.0\\.1)|" + "(localhost)|" + "(10\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})|"
            + "(172\\.((1[6-9])|(2\\d)|(3[01]))\\.\\d{1,3}\\.\\d{1,3})|"
            + "(192\\.168\\.\\d{1,3}\\.\\d{1,3})");
  }

  /**
   * 获取客户端IP对应网卡的MAC地址
   *
   * @param ipAddress ip
   * @return MAC地址
   */
  public static String getMACAddress(String ipAddress) {
    String str, strMAC = "", macAddress;
    try {
      Process pp = Runtime.getRuntime().exec("nbtstat -a " + ipAddress);
      InputStreamReader ir = new InputStreamReader(pp.getInputStream());
      LineNumberReader input = new LineNumberReader(ir);
      for (int i = 1; i < 100; i++) {
        str = input.readLine();
        if (str != null) {
          if (str.indexOf("MAC Address") > 1) {
            strMAC = str.substring(str.indexOf("MAC Address") + 14, str.length());
            break;
          }
        }
      }
    } catch (IOException ex) {
      return "Can't Get MAC Address!";
    }
    if (strMAC.length() < 17) {
      return "Error!";
    }

    macAddress =
        strMAC.substring(0, 2) + ":" + strMAC.substring(3, 5) + ":" + strMAC.substring(6, 8) + ":"
            + strMAC.substring(9, 11) + ":" + strMAC.substring(12, 14) + ":" + strMAC
            .substring(15, 17);
    return macAddress;
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy