import java.net.NetworkInterface;
import java.net.InetAddress;
public String getMacAddress() {
InetAddress ia = null;
byte[] mac = new byte[0];
try {
ia = InetAddress.getLocalHost();
mac = NetworkInterface.getByInetAddress(ia).getHardwareAddress();
} catch (Throwable e) {
e.printStackTrace();
return "";
}
StringBuffer buffer = new StringBuffer("");
for(int i=0; i<mac.length; i++) {
if(i!=0) {
buffer.append("-");
}
//字节转换为整数
int temp = mac[i]&0xff;
String str = Integer.toHexString(temp);
if(str.length()==1) {
buffer.append("0"+str);
}else {
buffer.append(str);
}
}
String aCase = buffer.toString().toUpperCase();
System.out.println(aCase);
return aCase;
}
java获取本机mac地址
最新推荐文章于 2024-07-20 02:55:01 发布