熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> JSP教程 >> 正文

利用Java獲取本機mac地址

2022-06-13   來源: JSP教程 

  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


From:http://tw.wingwit.com/Article/program/Java/JSP/201311/20036.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.