當前位置:首頁 » 編程語言 » 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