package org.liufei.jweb;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
public class MacAddress {
private static String hexByte(byte b) {
String s = "000000" + Integer.toHexString(b);
return s.substring(s.length() - 2);
}
public static String getHostMAC(String split) throws SocketException {
Enumeration<NetworkInterface> networkInterface_enumeration;
String mac_address = "";
networkInterface_enumeration = NetworkInterface.getNetworkInterfaces();
while (networkInterface_enumeration.hasMoreElements()) {
byte[] mac = networkInterface_enumeration.nextElement().getHardwareAddress();
if (mac == null)
continue;
mac_address = hexByte(mac[0]) + split + hexByte(mac[1]) + split
+ hexByte(mac[2]) + split + hexByte(mac[3]) + split
+ hexByte(mac[4]) + split + hexByte(mac[5]);
}
return mac_address;
}
public static void main(String[] args) throws SocketException {
System.out.println(MacAddress.getHostMAC(":"));
}
}
Java读取本地机器MAC地址
最新推荐文章于 2024-07-29 03:42:26 发布
本文介绍了一个Java程序,用于获取本地主机的MAC地址。通过遍历网络接口并读取硬件地址来实现这一目标。该程序使用了Java标准库中的NetworkInterface类。
1万+

被折叠的 条评论
为什么被折叠?



