首页 > 开发 > Java > 正文

java实现获取用户的MAC地址

2024-07-13 09:55:55
字体:
来源:转载
供稿:网友

本文给大家汇总介绍了下使用java实现获取客户端用户的MAC地址的方法,当然最后一种更全面一些,有需要的小伙伴们可以根据需求自由选择。

方法一:将本机地址与局域网内其他机器区分开来

 

 
  1. /** 
  2. * 根据IP地址获取mac地址 
  3. * @param ipAddress 127.0.0.1 
  4. * @return 
  5. * @throws SocketException 
  6. * @throws UnknownHostException 
  7. */ 
  8. public static String getLocalMac(String ipAddress) throws SocketException, 
  9. UnknownHostException { 
  10. // TODO Auto-generated method stub 
  11. String str = ""
  12. String macAddress = ""
  13. final String LOOPBACK_ADDRESS = "127.0.0.1"
  14. // 如果为127.0.0.1,则获取本地MAC地址。 
  15. if (LOOPBACK_ADDRESS.equals(ipAddress)) { 
  16. InetAddress inetAddress = InetAddress.getLocalHost(); 
  17. // 貌似此方法需要JDK1.6。 
  18. byte[] mac = NetworkInterface.getByInetAddress(inetAddress) 
  19. .getHardwareAddress(); 
  20. // 下面代码是把mac地址拼装成String 
  21. StringBuilder sb = new StringBuilder(); 
  22. for (int i = 0; i < mac.length; i++) { 
  23. if (i != 0) { 
  24. sb.append("-"); 
  25. // mac[i] & 0xFF 是为了把byte转化为正整数 
  26. String s = Integer.toHexString(mac[i] & 0xFF); 
  27. sb.append(s.length() == 1 ? 0 + s : s); 
  28. // 把字符串所有小写字母改为大写成为正规的mac地址并返回 
  29. macAddress = sb.toString().trim().toUpperCase(); 
  30. return macAddress; 
  31. else { 
  32. // 获取非本地IP的MAC地址 
  33. try { 
  34. System.out.println(ipAddress); 
  35. Process p = Runtime.getRuntime() 
  36. .exec("nbtstat -A " + ipAddress); 
  37. System.out.println("===process=="+p); 
  38. InputStreamReader ir = new InputStreamReader(p.getInputStream()); 
  39.  
  40. BufferedReader br = new BufferedReader(ir); 
  41.  
  42. while ((str = br.readLine()) != null) { 
  43. if(str.indexOf("MAC")>1){ 
  44. macAddress = str.substring(str.indexOf("MAC")+9, str.length()); 
  45. macAddress = macAddress.trim(); 
  46. System.out.println("macAddress:" + macAddress); 
  47. break
  48. p.destroy(); 
  49. br.close(); 
  50. ir.close(); 
  51. catch (IOException ex) { 
  52. return macAddress; 

我们再来看下方法二:

 

 
  1. package com.alpha.test; 
  2.  
  3. import java.io.BufferedReader; 
  4. import java.io.IOException; 
  5. import java.io.InputStream; 
  6. import java.io.InputStreamReader; 
  7. import java.io.LineNumberReader; 
  8.  
  9. public class GetMac { 
  10.  
  11. /** 
  12. * java获取客户端网卡的MAC地址 
  13.  
  14. * @param args 
  15. */ 
  16. public static void main(String[] args) { 
  17. GetMac get = new GetMac(); 
  18. System.out.println("1="+get.getMAC()); 
  19. System.out.println("2="+get.getMAC("127.0.0.1")); 
  20.  
  21. // 1.获取客户端ip地址( 这个必须从客户端传到后台): 
  22. // jsp页面下,很简单,request.getRemoteAddr() ; 
  23. // 因为系统的VIew层是用JSF来实现的,因此页面上没法直接获得类似request,在bean里做了个强制转换 
  24.  
  25. // public String getMyIP() { 
  26. // try { 
  27. // FacesContext fc = FacesContext.getCurrentInstance(); 
  28. // HttpServletRequest request = (HttpServletRequest) fc 
  29. // .getExternalContext().getRequest(); 
  30. // return request.getRemoteAddr(); 
  31. // } catch (Exception e) { 
  32. // e.printStackTrace(); 
  33. // } 
  34. // return ""; 
  35. // } 
  36.  
  37. // 2.获取客户端mac地址 
  38. // 调用window的命令,在后台Bean里实现 通过ip来获取mac地址。方法如下: 
  39.  
  40. // 运行速度【快】 
  41. public String getMAC() { 
  42. String mac = null
  43. try { 
  44. Process pro = Runtime.getRuntime().exec("cmd.exe /c ipconfig/all"); 
  45.  
  46. InputStream is = pro.getInputStream(); 
  47. BufferedReader br = new BufferedReader(new InputStreamReader(is)); 
  48. String message = br.readLine(); 
  49.  
  50. int index = -1; 
  51. while (message != null) { 
  52. if ((index = message.indexOf("Physical Address")) > 0) { 
  53. mac = message.substring(index + 36).trim(); 
  54. break
  55. message = br.readLine(); 
  56. System.out.println(mac); 
  57. br.close(); 
  58. pro.destroy(); 
  59. catch (IOException e) { 
  60. System.out.println("Can't get mac address!"); 
  61. return null
  62. return mac; 
  63.  
  64. // 运行速度【慢】 
  65. public String getMAC(String ip) { 
  66. String str = null
  67. String macAddress = null
  68. try { 
  69. Process p = Runtime.getRuntime().exec("nbtstat -A " + ip); 
  70. InputStreamReader ir = new InputStreamReader(p.getInputStream()); 
  71. LineNumberReader input = new LineNumberReader(ir); 
  72. for (; true;) { 
  73. str = input.readLine(); 
  74. if (str != null) { 
  75. if (str.indexOf("MAC Address") > 1) { 
  76. macAddress = str 
  77. .substring(str.indexOf("MAC Address") + 14); 
  78. break
  79. catch (IOException e) { 
  80. e.printStackTrace(System.out); 
  81. return null
  82. return macAddress; 

方法三,更精简一些

 

 
  1. import java.io.IOException; 
  2. import java.io.InputStreamReader; 
  3. import java.io.LineNumberReader; 
  4.  
  5. public class MACAddress { 
  6. String ip; 
  7. String mac; 
  8. public MACAddress (String ip){ 
  9. this.ip = ip; 
  10. public MACAddress (){ 
  11. this.ip = "0.0.0.0"
  12. public String getMac(){ 
  13. try { 
  14. Process p = Runtime.getRuntime().exec("arp -n"); 
  15. InputStreamReader ir = new InputStreamReader(p.getInputStream()); 
  16. LineNumberReader input = new LineNumberReader(ir); 
  17. p.waitFor(); 
  18. boolean flag = true
  19. String ipStr = "(" + this.ip + ")"
  20. while(flag) { 
  21. String str = input.readLine(); 
  22. if (str != null) { 
  23. if (str.indexOf(ipStr) > 1) { 
  24. int temp = str.indexOf("at"); 
  25. this.mac = str.substring( 
  26. temp + 3, temp + 20); 
  27. break
  28. else 
  29. flag = false
  30. catch (IOException | InterruptedException e) { 
  31. e.printStackTrace(System.out); 
  32. return this.mac; 
  33. public void setIp(String ip){ 
  34. this.ip = ip; 
  35.  

最后要放大招了,小伙伴们看仔细哦

首先要说的是:本方法可以支持外网机器的mac地址获取。

以前弄了一个只能访问局域网。 有防火墙就访问不了, 但是这个不用担心了。

测试了百度的ip,已经可以获得mac地址

java通过ip获取mac地址-封ip封mac地址

 

 
  1. import java.io.BufferedReader; 
  2. import java.io.InputStreamReader; 
  3. import java.util.regex.Matcher; 
  4. import java.util.regex.Pattern; 
  5.  
  6.  
  7. /** 
  8. * 获取MAC地址 
  9. * @author 
  10. * 2011-12 
  11. */ 
  12. public class GetMacAddress { 
  13. public static String callCmd(String[] cmd) {  
  14. String result = "";  
  15. String line = "";  
  16. try {  
  17. Process proc = Runtime.getRuntime().exec(cmd);  
  18. InputStreamReader is = new InputStreamReader(proc.getInputStream());  
  19. BufferedReader br = new BufferedReader (is);  
  20. while ((line = br.readLine ()) != null) {  
  21. result += line;  
  22. }  
  23. }  
  24. catch(Exception e) {  
  25. e.printStackTrace();  
  26. }  
  27. return result;  
  28.  
  29. /**  
  30.  
  31. * @param cmd 第一个命令  
  32. * @param another 第二个命令  
  33. * @return 第二个命令的执行结果  
  34. */ 
  35. public static String callCmd(String[] cmd,String[] another) {  
  36. String result = "";  
  37. String line = "";  
  38. try {  
  39. Runtime rt = Runtime.getRuntime();  
  40. Process proc = rt.exec(cmd);  
  41. proc.waitFor(); //已经执行完第一个命令,准备执行第二个命令  
  42. proc = rt.exec(another);  
  43. InputStreamReader is = new InputStreamReader(proc.getInputStream());  
  44. BufferedReader br = new BufferedReader (is);  
  45. while ((line = br.readLine ()) != null) {  
  46. result += line;  
  47. }  
  48. }  
  49. catch(Exception e) {  
  50. e.printStackTrace();  
  51. }  
  52. return result;  
  53.  
  54.  
  55.  
  56. /**  
  57.  
  58. * @param ip 目标ip,一般在局域网内  
  59. * @param sourceString 命令处理的结果字符串  
  60. * @param macSeparator mac分隔符号  
  61. * @return mac地址,用上面的分隔符号表示  
  62. */ 
  63. public static String filterMacAddress(final String ip, final String sourceString,final String macSeparator) {  
  64. String result = "";  
  65. String regExp = "((([0-9,A-F,a-f]{1,2}" + macSeparator + "){1,5})[0-9,A-F,a-f]{1,2})";  
  66. Pattern pattern = Pattern.compile(regExp);  
  67. Matcher matcher = pattern.matcher(sourceString);  
  68. while(matcher.find()){  
  69. result = matcher.group(1);  
  70. if(sourceString.indexOf(ip) <= sourceString.lastIndexOf(matcher.group(1))) {  
  71. break//如果有多个IP,只匹配本IP对应的Mac.  
  72. }  
  73.  
  74. return result;  
  75.  
  76.  
  77.  
  78. /**  
  79.  
  80. * @param ip 目标ip  
  81. * @return Mac Address  
  82.  
  83. */ 
  84. public static String getMacInWindows(final String ip){  
  85. String result = "";  
  86. String[] cmd = {  
  87. "cmd",  
  88. "/c",  
  89. "ping " + ip  
  90. };  
  91. String[] another = {  
  92. "cmd",  
  93. "/c",  
  94. "arp -a" 
  95. };  
  96.  
  97. String cmdResult = callCmd(cmd,another);  
  98. result = filterMacAddress(ip,cmdResult,"-");  
  99.  
  100. return result;  
  101. }  
  102.  
  103. /**  
  104. * @param ip 目标ip  
  105. * @return Mac Address  
  106.  
  107. */ 
  108. public static String getMacInLinux(final String ip){  
  109. String result = "";  
  110. String[] cmd = {  
  111. "/bin/sh",  
  112. "-c",  
  113. "ping " + ip + " -c 2 && arp -a" 
  114. };  
  115. String cmdResult = callCmd(cmd);  
  116. result = filterMacAddress(ip,cmdResult,":");  
  117.  
  118. return result;  
  119. }  
  120.  
  121. /** 
  122. * 获取MAC地址  
  123. * @return 返回MAC地址 
  124. */ 
  125. public static String getMacAddress(String ip){ 
  126. String macAddress = ""
  127. macAddress = getMacInWindows(ip).trim(); 
  128. if(macAddress==null||"".equals(macAddress)){ 
  129. macAddress = getMacInLinux(ip).trim(); 
  130. return macAddress; 
  131.  
  132. //做个测试 
  133. public static void main(String[] args) {  
  134. System.out.println(getMacAddress("220.181.111.148")); 
  135.  

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表