/*获取Mac地址*/
private static String getLocalMac(InetAddress ia) throws SocketException {
// TODO Auto-generated method stub
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);
}
}
return sb.toString().toUpperCase();
}
public static void main(String[] args) {
try{
//获取本机的InetAddress实例
InetAddress addr = InetAddress.getLocalHost();
System.out.println("本机MAC地址:" + getLocalMac(addr));
}catch (Exception e){
e.printStackTrace();
}
}
获取本机Mac的地址
最新推荐文章于 2022-02-23 13:38:51 发布