Java Web 获取客户端真实IP和MAC地址

获取IP地址:

/**
 * 根据请求获取IP地址
 *
 * @param request
 * @return
 */
public static String getIpAddressByRequest(HttpServletRequest request) {

    // 获取客户端ip地址
    String clientIp = request.getHeader("x-forwarded-for");

    if (null == clientIp || 0 == clientIp.length() || "unknown".equalsIgnoreCase(clientIp)) {
        clientIp = request.getRemoteAddr();
    }

    String[] clientIps = clientIp.split(",");
    if (clientIps.length <= 1) {
        return clientIp.trim();
    }

    // 判断是否来自CDN
    if (isComefromCDN(request)) {
        if (clientIps.length >= 2) {
            return clientIps[clientIps.length - 2].trim();
        }
    }

    return clientIps[clientIps.length - 1].trim();

}

private static boolean isComefromCDN(HttpServletRequest request) {
    String host = request.getHeader("host");
    return host.contains("www.189.cn") || host.contains("shouji.189.cn") || host.contains(
            "image2.chinatelecom-ec.com") || host.contains(
            "image1.chinatelecom-ec.com");
}

获取MAC地址:

/**
 * 根据IP地址获取mac地址(Windows)
 *
 * @param ipAddress 127.0.0.1
 * @return
 * @throws SocketException
 * @throws UnknownHostException
 */
public static String getLocalMacByIpAddress(String ipAddress) /*throws SocketException,
        UnknownHostException*/ {

    String loopbackAddress = "127.0.0.1";
    // 命令行结果
    String str = "";
    String macAddress = "";

    try {
        // 如果为127.0.0.1,则获取本地MAC地址。
        if (loopbackAddress.equals(ipAddress)) {

            InetAddress inetAddress = InetAddress.getLocalHost();

            // 貌似此方法需要JDK1.6。
            byte[] mac = NetworkInterface.getByInetAddress(inetAddress)
                    .getHardwareAddress();

            // 下面代码是把mac地址拼装成String
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < mac.length; i++) {
                if (i != 0) {
                    sb.append("-");
                }
                // mac[i] & 0xFF 是为了把byte转化为正整数
                String s = Integer.toHexString(mac[i] & 0xFF);
                sb.append(s.length() == 1 ? 0 + s : s);
            }

            // 把字符串所有小写字母改为大写成为正规的mac地址并返回
            macAddress = sb.toString().trim().toUpperCase();

        } else {

            // 获取非本地IP的MAC地址
            System.out.println(ipAddress);

            String cmd = "nbtstat -A " + ipAddress;

            File file = new File("C:\\Windows\\SysWOW64");
            if (file.exists()) {
                cmd = "C:\\Windows\\sysnative\\nbtstat.exe -A " + ipAddress;
            }

            Process p = Runtime.getRuntime()
                    .exec(cmd);

            System.out.println("===process==" + p);

            InputStreamReader ir = new InputStreamReader(p.getInputStream());

            BufferedReader br = new BufferedReader(ir);

            while ((str = br.readLine()) != null) {
                if (str.indexOf("MAC") > 1) {
                    macAddress = str.substring(str.indexOf("MAC") + 9, str.length());
                    macAddress = macAddress.trim();
                    System.out.println("macAddress:" + macAddress);
                    break;
                }
            }

            p.destroy();
            br.close();
            ir.close();

        }

    } catch (IOException ex) {

        return macAddress;
    }

    return macAddress;

}
发布了65 篇原创文章 · 获赞 8 · 访问量 16万+

猜你喜欢

转载自blog.csdn.net/u012382791/article/details/100072317