導航:首頁 > 編程語言 > java獲取客戶端mac

java獲取客戶端mac

發布時間:2025-10-17 03:31:22

java如何獲取mac地址

以windows舉例。x0dx0a運行命令" cmd ipconfig /all"就會出現以下結果x0dx0a x0dx0aPhysical Address. . . . . . . . . : 20-CF-30-9A-60-EEx0dx0a。x0dx0ajava就能過這樣的命令來獲取。以下是示例。x0dx0ax0dx0aimport java.io.BufferedReader;x0dx0aimport java.io.IOException;x0dx0aimport java.io.InputStreamReader;x0dx0ax0dx0apublic class TestMacx0dx0a{x0dx0a public static void main(String[] args) {x0dx0a System.out.println("Operation System=" + getOsName());x0dx0a System.out.println("Mac Address=" + getMACAddress());x0dx0a System.out.println("通過ip獲取mac"+getMACAddress("192.168.1.101"));x0dx0a }x0dx0ax0dx0a public static String getOsName() {x0dx0a String os = "";x0dx0a os = System.getProperty("os.name");x0dx0a return os;x0dx0a }x0dx0a x0dx0a public static String getMACAddress() {x0dx0a String address = "";x0dx0a String os = getOsName();x0dx0a if (os.startsWith("Windows")) {x0dx0a try {x0dx0a String command = "cmd.exe /c ipconfig /all";x0dx0a Process p = Runtime.getRuntime().exec(command);x0dx0a BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));x0dx0a String line;x0dx0a while ((line = br.readLine()) != null) {x0dx0a if (line.indexOf("Physical Address") > 0) {x0dx0a int index = line.indexOf(":");x0dx0a index += 2;x0dx0a address = line.substring(index);x0dx0a break;x0dx0a }x0dx0a }x0dx0a br.close();x0dx0a return address.trim();x0dx0a } catch (IOException e) {x0dx0a }x0dx0a } else if (os.startsWith("Linux")) {x0dx0a String command = "/bin/sh -c ifconfig -a";x0dx0a Process p;x0dx0a try {x0dx0a p = Runtime.getRuntime().exec(command);x0dx0a BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));x0dx0a String line;x0dx0a while ((line = br.readLine()) != null) {x0dx0a if (line.indexOf("HWaddr") > 0) {x0dx0a int index = line.indexOf("HWaddr") + "HWaddr".length();x0dx0a address = line.substring(index);x0dx0a break;x0dx0a }x0dx0a }x0dx0a br.close();x0dx0a } catch (IOException e) {x0dx0a }x0dx0a }x0dx0a address = address.trim();x0dx0a return address;x0dx0a }x0dx0ax0dx0apublic static String getMACAddress(String ipAddress) { x0dx0aString str = "", strMAC = "", macAddress = ""; x0dx0atry { x0dx0aProcess pp = Runtime.getRuntime().exec("nbtstat -a " + ipAddress); x0dx0aInputStreamReader ir = new InputStreamReader(pp.getInputStream()); x0dx0aLineNumberReader input = new LineNumberReader(ir); x0dx0afor (int i = 1; i < 100; i++) { x0dx0astr = input.readLine(); x0dx0aif (str != null) { x0dx0aif (str.indexOf("MAC Address") > 1) { x0dx0astrMAC = str.substring(str.indexOf("MAC Address") + 14, x0dx0astr.length()); x0dx0abreak; x0dx0a} x0dx0a} x0dx0a} x0dx0a} catch (IOException ex) { x0dx0areturn "Can't Get MAC Address!"; x0dx0a} x0dx0a// x0dx0aif (strMAC.length() < 17) { x0dx0areturn "Error!"; x0dx0a} x0dx0ax0dx0amacAddress = strMAC.substring(0, 2) + ":" + strMAC.substring(3, 5) x0dx0a+ ":" + strMAC.substring(6, 8) + ":" + strMAC.substring(9, 11) x0dx0a+ ":" + strMAC.substring(12, 14) + ":" x0dx0a+ strMAC.substring(15, 17); x0dx0a// x0dx0areturn macAddress; x0dx0a} x0dx0a} x0dx0ax0dx0a劍天夢的回答原理和我這個一樣,都是通過Process 執行命令。 我直接補充到答案里了。不過x0dx0a我這邊運行那個命令出來的結果很多,那麼花的時間就長了。優點是能夠獲取別人的mac地址 。

❷ Java獲取電腦mac地址,如果是雙網卡獲取的mac地址是不是隨機的

取mac要先得到NetworkInterface對象,多網卡的話會有多個對象,所以要看你在哪個對象上取mac了,反正不會是隨機

❸ Java web 怎麼得到客戶端的Mac地址

這個跟瀏覽器有關,你的js許可權要向瀏覽器申請,瀏覽器向操作系統申請

現在的瀏覽器並沒有開放這個許可權.

解決辦法1:用C++寫GUI,調用操作系統的API,獲取mac地址,每個操作系統的API是不一樣的.

解決辦法2:自己寫一個瀏覽器,並且寫好js的api,然後瀏覽器去請求系統調用,獲得mac地址.

所謂的active插件一樣也是向操作系統申請了api獲取的

❹ java如何獲取mac地址

解釋說明可參考代碼中的注釋即可:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;

public class GetMac {

/**
* java獲取客戶端網卡的MAC地址
*
* @param args
*/
public static void main(String[] args) {
GetMac get = new GetMac();
System.out.println("1="+get.getMAC());
System.out.println("2="+get.getMAC("127.0.0.1"));
}

// 1.獲取客戶端ip地址( 這個必須從客戶端傳到後台):
// jsp頁面下,很簡單,request.getRemoteAddr() ;
// 因為系統的VIew層是用JSF來實現的,因此頁面上沒法直接獲得類似request,在bean里做了個強制轉換

// public String getMyIP() {
// try {
// FacesContext fc = FacesContext.getCurrentInstance();
// HttpServletRequest request = (HttpServletRequest) fc
// .getExternalContext().getRequest();
// return request.getRemoteAddr();
// } catch (Exception e) {
// e.printStackTrace();
// }
// return "";
// }

// 2.獲取客戶端mac地址
// 調用window的命令,在後台Bean里實現 通過ip來獲取mac地址。方法如下:

// 運行速度【快】
public String getMAC() {
String mac = null;
try {
Process pro = Runtime.getRuntime().exec("cmd.exe /c ipconfig/all");

InputStream is = pro.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String message = br.readLine();

int index = -1;
while (message != null) {
if ((index = message.indexOf("Physical Address")) > 0) {
mac = message.substring(index + 36).trim();
break;
}
message = br.readLine();
}
System.out.println(mac);
br.close();
pro.destroy();
} catch (IOException e) {
System.out.println("Can't get mac address!");
return null;
}
return mac;
}

// 運行速度【慢】
public String getMAC(String ip) {
String str = null;
String macAddress = null;
try {
Process p = Runtime.getRuntime().exec("nbtstat -A " + ip);
InputStreamReader ir = new InputStreamReader(p.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
for (; true;) {
str = input.readLine();
if (str != null) {
if (str.indexOf("MAC Address") > 1) {
macAddress = str
.substring(str.indexOf("MAC Address") + 14);
break;
}
}
}
} catch (IOException e) {
e.printStackTrace(System.out);
return null;
}
return macAddress;
}
}

閱讀全文

與java獲取客戶端mac相關的資料

熱點內容
不想學編程選什麼專業 瀏覽:97
華為g610一t11大聲音rom版本下載 瀏覽:645
背景音樂通常採用wav格式的音樂文件 瀏覽:218
java程序員提高班 瀏覽:67
為什麼要發明可編程式控制制器 瀏覽:846
電腦顯示佔用空間找不到文件 瀏覽:963
nxtf格式的文件是否可以列印 瀏覽:465
渤海銀行app 瀏覽:606
jsp多個條件查詢 瀏覽:400
蘋果數據線哪個牌子好2021 瀏覽:481
3dmax劍製作教程 瀏覽:1000
amesim安裝程序找不到文件 瀏覽:629
java獲取客戶端mac 瀏覽:579
linux本地連接資料庫 瀏覽:594
微信接收多大壓縮文件 瀏覽:972
vba打開文件 瀏覽:525
delphi文件大小 瀏覽:15
jqueryjson轉義 瀏覽:142
華碩筆記本系統找不到指定文件 瀏覽:475
安卓目錄管理工具 瀏覽:502

友情鏈接