public static void main(String[] args) throws UnknownHostException, SocketException {
//得到IP,输出主机/ip地址
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) {
//每2位用“-”拼接
sb.append("-");
}
//字节转换为整数
int temp = mac[i] & 0xff;
String str = Integer.toHexString(temp);
if(str.length()==1) {
//十位补0
sb.append("0"+str);
}else {
sb.append(str);
}
}
System.out.println("本机MAC地址:"+sb.toString().toUpperCase());
}
java获取本机MAC地址
最新推荐文章于 2024-04-03 08:00:00 发布