小编Yuv*_*hen的帖子

Java获取断开连接的卡的MAC地址

我想获取使用此解决方案并在此处搜索的以太网卡的MAC(用于产品密钥),问题是当所有网络(以太网和wifi)断开连接时,它返回空的MAC地址。我宁愿获得以太网地址,即使它已断开连接也是如此。

谢谢!!

   public static void main(String[] args)
    {
        InetAddress ip;
        try {
            ip = InetAddress.getLocalHost();

            System.out.println("The mac Address of this machine is :" + ip.getHostAddress());

            NetworkInterface network = NetworkInterface.getByInetAddress(ip);

            byte[] mac = network.getHardwareAddress();

            System.out.print("The mac address is : ");

            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < mac.length; i++){
                sb.append(String.format("%02X%s", mac[i],(i< mac.length - 1)?"-":""));
            }

            System.out.println(sb.toString());

        } 
        catch (UnknownHostException e) {
            e.printStackTrace();
        } 
        catch (SocketException e) {
            e.printStackTrace();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

java networking

17
推荐指数
1
解决办法
2635
查看次数

标签 统计

java ×1

networking ×1