javagetby
发布时间: 2023-03-22 02:41:00
‘壹’ java怎么生成本机mac地址
来走一个
publicstaticvoidmain(String[]args)throwsUnknownHostException,SocketException{
InetAddressinetAddress=InetAddress.getLocalHost();
//获取网卡,获取地址
byte[]mac=NetworkInterface.getByInetAddress(inetAddress).getHardwareAddress();
StringBuffersb=newStringBuffer("");
for(inti=0;i<mac.length;i++){
if(i!=0){
sb.append("-");
}
//字节转换为整数
inttemp=mac[i]&0xff;
Stringstr=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
‘贰’ java中怎么获取电脑的mac地址
importjava.net.InetAddress;
importjava.net.NetworkInterface;
importjava.net.SocketException;
importjava.net.UnknownHostException;
/*
*物理地址是48位,别和ipv6搞错了
*/
publicclassLOCALMAC{
/**
*@paramargs
*@throwsUnknownHostException
*@throwsSocketException
*/
publicstaticvoidmain(String[]args)throwsUnknownHostException,SocketException{
//TODOAuto-generatedmethodstub
//得到IP,输出PC-201309011313/122.206.73.83
InetAddressia=InetAddress.getLocalHost();
System.out.println(ia);
getLocalMac(ia);
}
privatestaticvoidgetLocalMac(InetAddressia)throwsSocketException{
//TODOAuto-generatedmethodstub
//获取网卡,获取地址
byte[]mac=NetworkInterface.getByInetAddress(ia).getHardwareAddress();
System.out.println("mac数组长度:"+mac.length);
StringBuffersb=newStringBuffer("");
for(inti=0;i<mac.length;i++){
if(i!=0){
sb.append("-");
}
//字节转换为整数
inttemp=mac[i]&0xff;
Stringstr=Integer.toHexString(temp);
System.out.println("每8位:"+str);
if(str.length()==1){
sb.append("0"+str);
}else{
sb.append(str);
}
}
System.out.println("本机MAC地址:"+sb.toString().toUpperCase());
}
}
热点内容