java

位置:IT落伍者 >> java >> 浏览文章

利用Java获取本机mac地址


发布日期:2018年05月03日
 
利用Java获取本机mac地址

public static void getAllMacAdress() {        Enumeration<NetworkInterface> netInterfaces = null;         try {            // 获得所有网络接口            netInterfaces = NetworkInterfacegetNetworkInterfaces();            while (netInterfaceshasMoreElements()) {                Systemout                        println("==============================================");                String mac = "";                StringBuffer sb = new StringBuffer();                NetworkInterface ni = netInterfacesnextElement();                Systemoutprintln("DisplayName: " + nigetDisplayName());                Systemoutprintln("Name: " + nigetName());                 byte[] macs = nigetHardwareAddress();                // 该interface不存在HardwareAddress继续下一次循环                if (macs == null) {                    continue;                }                 for (int i = ; i < macslength; i++) {                    mac = IntegertoHexString(macs[i] & xFF);                    if (maclength() == ) {                        mac = + mac;                    }                    sbappend(mac + "");                }                mac = sbtoString();                mac = macsubstring( maclength() );                Systemoutprintln(mac);                 Enumeration<InetAddress> ips = nigetInetAddresses();                while (ipshasMoreElements()) {                    Systemoutprintln("IP: "                            + ipsnextElement()getHostAddress());                }            }        } catch (SocketException e) {            eprintStackTrace();        }    }

本方法需要使用使用jdk

               

上一篇:Java初学入门需掌握的30个概念

下一篇:java程序操作MySQL数据库