javaWeb快速获取服务器的IP和对应的Mac地址

/**
* 是,如果通过了多级反向代理的话,X-Forwarded-For的值并不止一个,而是一串IP值,究竟哪个才是真正的用户端的真实IP呢? 答案是取X-Forwarded-For中第一个非unknown的有效IP字符串。 如:X-Forwarded-For:192.168.1.110, 192.168.1.120, 192.168.1.130, 192.168.1.100用户真实IP为:
* 192.168.1.110

* @参照 http://suoyihen.iteye.com/blog/1355934

* @return 获得客户端的真实IP地址
* @throws SocketException 
* @throws UnknownHostException 
*/
public static String getIPAddress() {
if (ServletActionContext.getRequest() == null)
return "";
String ip = ServletActionContext.getRequest().getHeader("X-Requested-For");
if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
ip = ServletActionContext.getRequest().getHeader("X-Forwarded-For");
}
if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
ip = ServletActionContext.getRequest().getHeader("Proxy-Client-IP");
}
if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
ip = ServletActionContext.getRequest().getHeader("WL-Proxy-Client-IP");
}
if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
ip = ServletActionContext.getRequest().getHeader("HTTP_CLIENT_IP");
}
if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
ip = ServletActionContext.getRequest().getHeader("HTTP_X_FORWARDED_FOR");
}
if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
ip = ServletActionContext.getRequest().getRemoteAddr();
}
if ("127.0.0.1".equals(ip) || "0:0:0:0:0:0:0:1".equals(ip))
       try {
        //根据网卡取本机配置的IP  
        ip = InetAddress.getLocalHost().getHostAddress();
       }
       catch (UnknownHostException unknownhostexception) {
       }
//对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割  
        if(ip!=null && ip.length()>15){ //"***.***.***.***".length() = 15  
            if(ip.indexOf(",")>0){  
            ip = ip.substring(0,ip.indexOf(","));  
            }  
        } 
return ip;

}


//获取对应的mac地址

public static String getMACAddress2(String ip) {
       String str = "";
       String macAddress = "";
       try {
           Process p = Runtime.getRuntime().exec("nbtstat -a " + ip);
           InputStreamReader ir = new InputStreamReader(p.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) {
                   if (str.indexOf("MAC") > 1) {
                       macAddress = str.substring(
                               str.indexOf("=") + 2, str.length());
                       break;
                   }
               }
           }
       } catch (IOException e) {
           e.printStackTrace(System.out);
       }
       return macAddress;
   }


你可能感兴趣的:(JavaEE)