本文目錄一覽:
java怎麼獲取系統mac地址
首先,創建工程,包,和一個類。
在此不加詳述,我們直接看代碼。
這裡,我把這個類命名為GetMacAddr
這裡,最最關鍵的就是這裡這個方法。
我們通過NetworkInterface這個類來操作。
也就是通過getLocalHost()方法先得到本機IP,
然後調用getHardwareAddress()方法得到一個byte數組的地址。
我們把六位地址傳到一個byte數組裡面,然後輸出來就是。
不多廢話,看代碼:
private void getMACAddr()
throws SocketException, UnknownHostException {
// 獲得IP
NetworkInterface netInterface =
NetworkInterface.getByInetAddress(InetAddress.getLocalHost());
// 獲得Mac地址的byte數組
byte[] macAddr = netInterface.getHardwareAddress();
System.out.print(“MAC Addr:\t”);
// 循環輸出
for (byte b : macAddr) {
// 這裡的toHexString()是自己寫的格式化輸出的方法,見下步。
System.out.print(toHexString(b) + ” “);
}
}
上一步驟中,為什麼會出現一個toHexString()方法呢?
因為可能10進制轉16進制時候可能會出現單字符,
所以,如果有出現單字符的情況,我們在其前面添加一個“0”做佔位符。
這也是為了視覺的直觀,也夾帶着個人的習慣。
private static String toHexString(int integer) {
// 將得來的int類型數字轉化為十六進制數
String str = Integer.toHexString((int) (integer 0xff));
// 如果遇到單字符,前置0佔位補滿兩格
if (str.length() == 1) {
str = “0” + str;
}
return str;
}
java如何獲取mac地址?
以windows舉例。\x0d\x0a運行命令” cmd ipconfig /all”就會出現以下結果\x0d\x0a \x0d\x0aPhysical Address. . . . . . . . . : 20-CF-30-9A-60-EE\x0d\x0a。\x0d\x0ajava就能過這樣的命令來獲取。以下是示例。\x0d\x0a\x0d\x0aimport java.io.BufferedReader;\x0d\x0aimport java.io.IOException;\x0d\x0aimport java.io.InputStreamReader;\x0d\x0a\x0d\x0apublic class TestMac\x0d\x0a{\x0d\x0a public static void main(String[] args) {\x0d\x0a System.out.println(“Operation System=” + getOsName());\x0d\x0a System.out.println(“Mac Address=” + getMACAddress());\x0d\x0a System.out.println(“通過ip獲取mac”+getMACAddress(“192.168.1.101”));\x0d\x0a }\x0d\x0a\x0d\x0a public static String getOsName() {\x0d\x0a String os = “”;\x0d\x0a os = System.getProperty(“os.name”);\x0d\x0a return os;\x0d\x0a }\x0d\x0a \x0d\x0a public static String getMACAddress() {\x0d\x0a String address = “”;\x0d\x0a String os = getOsName();\x0d\x0a if (os.startsWith(“Windows”)) {\x0d\x0a try {\x0d\x0a String command = “cmd.exe /c ipconfig /all”;\x0d\x0a Process p = Runtime.getRuntime().exec(command);\x0d\x0a BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));\x0d\x0a String line;\x0d\x0a while ((line = br.readLine()) != null) {\x0d\x0a if (line.indexOf(“Physical Address”) 0) {\x0d\x0a int index = line.indexOf(“:”);\x0d\x0a index += 2;\x0d\x0a address = line.substring(index);\x0d\x0a break;\x0d\x0a }\x0d\x0a }\x0d\x0a br.close();\x0d\x0a return address.trim();\x0d\x0a } catch (IOException e) {\x0d\x0a }\x0d\x0a } else if (os.startsWith(“Linux”)) {\x0d\x0a String command = “/bin/sh -c ifconfig -a”;\x0d\x0a Process p;\x0d\x0a try {\x0d\x0a p = Runtime.getRuntime().exec(command);\x0d\x0a BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));\x0d\x0a String line;\x0d\x0a while ((line = br.readLine()) != null) {\x0d\x0a if (line.indexOf(“HWaddr”) 0) {\x0d\x0a int index = line.indexOf(“HWaddr”) + “HWaddr”.length();\x0d\x0a address = line.substring(index);\x0d\x0a break;\x0d\x0a }\x0d\x0a }\x0d\x0a br.close();\x0d\x0a } catch (IOException e) {\x0d\x0a }\x0d\x0a }\x0d\x0a address = address.trim();\x0d\x0a return address;\x0d\x0a }\x0d\x0a\x0d\x0apublic static String getMACAddress(String ipAddress) { \x0d\x0aString str = “”, strMAC = “”, macAddress = “”; \x0d\x0atry { \x0d\x0aProcess pp = Runtime.getRuntime().exec(“nbtstat -a ” + ipAddress); \x0d\x0aInputStreamReader ir = new InputStreamReader(pp.getInputStream()); \x0d\x0aLineNumberReader input = new LineNumberReader(ir); \x0d\x0afor (int i = 1; i str = input.readLine(); \x0d\x0aif (str != null) { \x0d\x0aif (str.indexOf(“MAC Address”) 1) { \x0d\x0astrMAC = str.substring(str.indexOf(“MAC Address”) + 14, \x0d\x0astr.length()); \x0d\x0abreak; \x0d\x0a} \x0d\x0a} \x0d\x0a} \x0d\x0a} catch (IOException ex) { \x0d\x0areturn “Can’t Get MAC Address!”; \x0d\x0a} \x0d\x0a// \x0d\x0aif (strMAC.length() return “Error!”; \x0d\x0a} \x0d\x0a\x0d\x0amacAddress = strMAC.substring(0, 2) + “:” + strMAC.substring(3, 5) \x0d\x0a+ “:” + strMAC.substring(6, 8) + “:” + strMAC.substring(9, 11) \x0d\x0a+ “:” + strMAC.substring(12, 14) + “:” \x0d\x0a+ strMAC.substring(15, 17); \x0d\x0a// \x0d\x0areturn macAddress; \x0d\x0a} \x0d\x0a} \x0d\x0a\x0d\x0a劍天夢的回答原理和我這個一樣,都是通過Process 執行命令。 我直接補充到答案里了。不過\x0d\x0a我這邊運行那個命令出來的結果很多,那麼花的時間就長了。優點是能夠獲取別人的mac地址 。
java中怎麼獲取電腦的mac地址
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
/*
* 物理地址是48位,別和ipv6搞錯了
*/
public class LOCALMAC {
/**
* @param args
* @throws UnknownHostException
* @throws SocketException
*/
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; imac.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());
}
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/227590.html