java怎么生成本机mac地址
发布网友
发布时间:2022-03-26 23:28
我来回答
共1个回答
热心网友
时间:2022-03-27 00:58
来走一个
public static void main(String[] args) throws UnknownHostException,SocketException{
InetAddress inetAddress = InetAddress.getLocalHost();
//获取网卡,获取地址
byte[] mac = NetworkInterface.getByInetAddress(inetAddress).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);
}
}
System.out.println("本机MAC地址:" + sb.toString().toUpperCase());
}
输出:
本机MAC地址:B8-88-E3-FA-03-39