Java获取mac地址

    public static Map getLocalMacMap()
        throws SocketException
    {
        Map macs = new HashMap();
        for(Enumeration networks = NetworkInterface.getNetworkInterfaces(); networks.hasMoreElements();)
        {
            NetworkInterface ni = (NetworkInterface)networks.nextElement();
            if(!ni.isVirtual() && !ni.isLoopback() && ni.supportsMulticast() && !ni.isPointToPoint())
            {
                byte data[] = ni.getHardwareAddress();
                if(data != null && data.length > 0)
                {
                    String name = ni.getDisplayName();
                    String mac = parseMac(data);
                    if(!macs.containsValue(mac))
                    {
                        if(name == null || name.length() < 1)
                        {
                            StringBuilder sb = new StringBuilder();
                            sb.append("Common Network Connection");
                            sb.append("(Mac: ").append(mac).append(")");
                            name = sb.toString();
                        }
                        macs.put(name, mac);
                    }
                }
            }
        }

        return macs;
    }
 

你可能感兴趣的:(java)