Java 获取本机MAC地址
public static void main(String
[] args
) {
String localMAC
= getLocalMac();
System
.out
.println("MAC地址为: " + localMAC
);
}
public static String
getLocalMac() {
try {
InetAddress ia
= InetAddress
.getLocalHost();
byte
[] mac
= NetworkInterface
.getByInetAddress(ia
).getHardwareAddress();
StringBuffer sb
= new StringBuffer("");
for (int i
= 0; i
< mac
.length
; i
++) {
if (i
!= 0) {
sb
.append("-");
}
int temp
= mac
[i
] & 0xff;
String str
= Integer
.toHexString(temp
);
if (str
.length() == 1) {
sb
.append("0" + str
);
} else {
sb
.append(str
);
}
}
String localMAC
= sb
.toString().toUpperCase();
return localMAC
;
} catch (Exception e
) {
e
.printStackTrace();
}
return null;
}
亲测可用…
转载请注明原文地址: https://lol.8miu.com/read-32628.html