java获取用户的mac地址

1、java获取用户的mac地址,网上找的代码测试后都不能够获取到客户的mac地址

阅读 4.4k
4 个回答

可能需要管理员权限才能获取。试着以管理员权限执行。如果是命令行里,就先以管理员权限打开命令行。

你的客户是怎样的客户,机器是怎样的,系统是什么?把问题描述清楚,再来谈你的问题。

public static void main(String[] args) throws UnknownHostException, SocketException {        
    //得到IP,输出PC-201309011313/122.206.73.83
    InetAddress ia = InetAddress.getLocalHost();
    System.out.println(ia);
    getLocalMac(ia);
}
private static void getLocalMac(InetAddress ia) throws SocketException {
    //获取网卡,获取地址
    byte[] mac = NetworkInterface.getByInetAddress(ia).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);
        }
    }
    System.out.println("本机MAC地址:"+sb.toString().toUpperCase());
}
推荐问题