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