`

获取IP地址与MAC地址

阅读更多
/** 
2. * 通过HttpServletRequest返回IP地址 
3. * @param request HttpServletRequest 
4. * @return ip String 
5. * @throws Exception 
6. */ 
7.public String getIpAddr(HttpServletRequest request) throws Exception {  
8.    String ip = request.getHeader("x-forwarded-for");  
9.    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
10.        ip = request.getHeader("Proxy-Client-IP");  
11.    }  
12.    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
13.        ip = request.getHeader("WL-Proxy-Client-IP");  
14.    }  
15.    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
16.        ip = request.getHeader("HTTP_CLIENT_IP");  
17.    }  
18.    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
19.        ip = request.getHeader("HTTP_X_FORWARDED_FOR");  
20.    }  
21.    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
22.        ip = request.getRemoteAddr();  
23.    }  
24.    return ip;  
25.} 
通过Ip地址获取MAC地址
/** 
2. * 通过IP地址获取MAC地址 
3. * @param ip String,127.0.0.1格式 
4. * @return mac String 
5. * @throws Exception 
6. */ 
7.public String getMACAddress(String ip) throws Exception {  
8.    String line = "";  
9.    String macAddress = "";  
10.    final String MAC_ADDRESS_PREFIX = "MAC Address = ";  
11.    final String LOOPBACK_ADDRESS = "127.0.0.1";  
12.    //如果为127.0.0.1,则获取本地MAC地址。  
13.    if (LOOPBACK_ADDRESS.equals(ip)) {  
14.        InetAddress inetAddress = InetAddress.getLocalHost();  
15.        //貌似此方法需要JDK1.6。  
16.        byte[] mac = NetworkInterface.getByInetAddress(inetAddress).getHardwareAddress();  
17.        //下面代码是把mac地址拼装成String  
18.        StringBuilder sb = new StringBuilder();  
19.        for (int i = 0; i < mac.length; i++) {  
20.            if (i != 0) {  
21.                sb.append("-");  
22.            }  
23.            //mac[i] & 0xFF 是为了把byte转化为正整数  
24.            String s = Integer.toHexString(mac[i] & 0xFF);  
25.            sb.append(s.length() == 1 ? 0 + s : s);  
26.        }  
27.        //把字符串所有小写字母改为大写成为正规的mac地址并返回  
28.        macAddress = sb.toString().trim().toUpperCase();  
29.        return macAddress;  
30.    }  
31.    //获取非本地IP的MAC地址  
32.    try {  
33.        Process p = Runtime.getRuntime().exec("nbtstat -A " + ip);  
34.        InputStreamReader isr = new InputStreamReader(p.getInputStream());  
35.        BufferedReader br = new BufferedReader(isr);  
36.        while ((line = br.readLine()) != null) {  
37.            if (line != null) {  
38.                int index = line.indexOf(MAC_ADDRESS_PREFIX);  
39.                if (index != -1) {  
40.                    macAddress = line.substring(index + MAC_ADDRESS_PREFIX.length()).trim().toUpperCase();  
41.                }  
42.            }  
43.        }  
44.        br.close();  
45.    } catch (IOException e) {  
46.        e.printStackTrace(System.out);  
47.    }  
48.    return macAddress;  
49.} 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics