MAC地址也就是物理地址
所以cmd电脑查看
一、自定义方法获取本地mac地址
private static String getLocalMac(InetAddress inetAddress) { try { //获取网卡,获取地址 byte[] mac = NetworkInterface.getByInetAddress(inetAddress).getHardwareAddress(); StringBuffer sb = new StringBuffer(); for (int i = 0; i < mac.length; i++) { if (i != 0) { sb.append("-"); } //字节转换为整数 int temp = mac[i] & 0xff; String str = Integer.toHexString(temp); if (str.length() == 1) { sb.append("0" + str); } else { sb.append(str); } } return sb.toString(); } catch (Exception exception) { } return null; }
二、第三方工具类获取本地mac地址
需要引入hutool依赖
cn.hutool hutool-all 5.4.0
InetAddress inetAddress = InetAddress.getLocalHost();
第二种方式:利用hutool工具类中的封装方法获取本机mac地址
String localMacAddress2 = NetUtil.getMacAddress(inetAddress);System.out.println("localMacAddress2 = " + localMacAddress2);
三、两种方法运行
public static void main(String[] args) throws UnknownHostException { InetAddress inetAddress = InetAddress.getLocalHost(); //第一种方式:利用自己写的方法获取本地mac地址 String localMacAddress1 = getLocalMac(inetAddress); System.out.println("localMacAddress1 = " + localMacAddress1); //第二种方式:利用hutool工具类中的封装方法获取本机mac地址 String localMacAddress2 = NetUtil.getMacAddress(inetAddress); System.out.println("localMacAddress2 = " + localMacAddress2); }
结果
来源地址:https://blog.csdn.net/LRXmrlirixing/article/details/127281628