java获取电脑mac物理地址
生活随笔
收集整理的這篇文章主要介紹了
java获取电脑mac物理地址
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
public static void main(String[] args) throws UnknownHostException, SocketException {
// TODO Auto-generated method stub
//得到IP,輸出PC-201309011313/122.206.73.83
InetAddress ia = InetAddress.getLocalHost();
System.out.println(ia);
getLocalMac(ia);
}
private static void getLocalMac(InetAddress ia) throws SocketException {
// TODO Auto-generated method stub
//獲取網卡,獲取地址
byte[] mac = NetworkInterface.getByInetAddress(ia).getHardwareAddress();
System.out.println("mac數組長度:"+mac.length);
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);
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获取电脑mac物理地址的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: iis创建php网站,iis怎么搭建ph
- 下一篇: Android 如何修改默认输入法