import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Date;
import java.util.Enumeration;
public class HardwareUtil {
public static String getMac() {
StringBuffer buf = new StringBuffer();
try {
Enumeration<NetworkInterface> el = NetworkInterface
.getNetworkInterfaces();
InetAddress ip = null;
while (el.hasMoreElements()) {
NetworkInterface ni = (NetworkInterface) el.nextElement();
byte[] mac = ni.getHardwareAddress();
if (mac == null || mac.length == 0)
continue;
Enumeration<InetAddress> nii = ni.getInetAddresses();
while (nii.hasMoreElements()) {
ip = nii.nextElement();
if (ip instanceof Inet6Address)
continue;
if (!ip.isReachable(3000))
continue;
for (byte b : mac) {
buf.append(b + "-");
}
buf.append("&");
}
}
} catch (Exception e) {
e.printStackTrace();
}
return buf.toString();
}
public static void main(String[] arg){
String mac = HardwareUtil.getMac();
System.out.println("mac:"+mac);
}
}
Java获取本机Mac地址
最新推荐文章于 2024-08-14 04:40:37 发布