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());
}
}
熱點內容