当前位置:首页 » 编程语言 » javagetby

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());

}

}

热点内容
java返回this 发布:2025-10-20 08:28:16 浏览:714
制作脚本网站 发布:2025-10-20 08:17:34 浏览:982
python中的init方法 发布:2025-10-20 08:17:33 浏览:688
图案密码什么意思 发布:2025-10-20 08:16:56 浏览:840
怎么清理微信视频缓存 发布:2025-10-20 08:12:37 浏览:748
c语言编译器怎么看执行过程 发布:2025-10-20 08:00:32 浏览:1088
邮箱如何填写发信服务器 发布:2025-10-20 07:45:27 浏览:315
shell脚本入门案例 发布:2025-10-20 07:44:45 浏览:194
怎么上传照片浏览上传 发布:2025-10-20 07:44:03 浏览:883
python股票数据获取 发布:2025-10-20 07:39:44 浏览:841