IPutils获取本机mac

朴灿烈づ我的快乐病毒、 2022-08-27 12:55 258阅读 0赞
  1. package com.ctl.util;
  2. import java.net.InetAddress;
  3. import java.net.NetworkInterface;
  4. import java.net.SocketException;
  5. import java.net.UnknownHostException;
  6. /**
  7. *
  8. * @author Administrator
  9. *
  10. */
  11. public class IPutils {
  12. /**
  13. * 获取mac地址
  14. * @return mac
  15. * @throws SocketException
  16. * @throws UnknownHostException
  17. */
  18. private static String getLocalMac() throws SocketException, UnknownHostException {
  19. //获取网卡,获取地址
  20. byte[] mac = NetworkInterface.getByInetAddress(InetAddress.getLocalHost()).getHardwareAddress();
  21. //System.out.println("mac数组长度:"+mac.length);
  22. StringBuffer sb = new StringBuffer("");
  23. for(int i=0; i<mac.length; i++) {
  24. if(i!=0) {
  25. sb.append("-");
  26. }
  27. //字节转换为整数
  28. int temp = mac[i]&0xff;
  29. String str = Integer.toHexString(temp);
  30. //System.out.println("每8位:"+str);
  31. if(str.length()==1) {
  32. sb.append("0"+str);
  33. }else {
  34. sb.append(str);
  35. }
  36. }
  37. return sb.toString().toUpperCase();
  38. }
  39. }

发表评论

表情:
评论列表 (有 0 条评论,258人围观)

还没有评论,来说两句吧...

相关阅读

    相关 修改mac

    公司给我网限速了,从svn上拉个包慢得要死~~~ 找到下图位置右键 ![1404340-20190717091759947-2135604482.png][]   ![