在路上

 找回密码
 立即注册
在路上 站点首页 学习 查看内容

Java获取本地机器MAC地址

2016-12-20 13:15| 发布者: zhangjf| 查看: 437| 评论: 0

摘要: import java.net.NetworkInterface;import java.net.SocketException;import java.util.Enumeration;/** * * @author MrWang * 获取客户端MAC地址 * * */public class MacAddress { public static String hexBy ...
  1. import java.net.NetworkInterface;
  2. import java.net.SocketException;
  3. import java.util.Enumeration;
  4. /**
  5. *
  6. * @author MrWang
  7. * 获取客户端MAC地址
  8. *
  9. *
  10. */
  11. public class MacAddress {
  12. public static String hexByte(byte b) {
  13. String s = "000000" + Integer.toHexString(b);
  14. return s.substring(s.length() - 2);
  15. }
  16. public static String getMAC() {
  17. Enumeration<NetworkInterface> el;
  18. String mac_s = "";
  19. try {
  20. el = NetworkInterface.getNetworkInterfaces();
  21. while (el.hasMoreElements()) {
  22. byte[] mac = el.nextElement().getHardwareAddress();
  23. if (mac == null)
  24. continue;
  25. mac_s = hexByte(mac[0]) + "-" + hexByte(mac[1]) + "-"
  26. + hexByte(mac[2]) + "-" + hexByte(mac[3]) + "-"
  27. + hexByte(mac[4]) + "-" + hexByte(mac[5]);
  28. System.out.println(mac_s + "MAC地址");
  29. }
  30. } catch (SocketException e1) {
  31. e1.printStackTrace();
  32. }
  33. return mac_s;
  34. }
  35. public static void main(String[] args) {
  36. MacAddress m = new MacAddress();
  37. m.getMAC();
  38. }
  39. }
复制代码

最新评论

小黑屋|在路上 ( 蜀ICP备15035742号-1 

;

GMT+8, 2025-7-8 15:20

Copyright 2015-2025 djqfx

返回顶部