當前位置:首頁 » 編程語言 » java取mac地址

java取mac地址

發布時間: 2022-10-04 04:15:47

Ⅰ 如何獲得本機mac地址 java

importjava.net.InetAddress;
importjava.net.NetworkInterface;
importjava.net.SocketException;
importjava.net.UnknownHostException;

publicclassApp{

publicstaticvoidmain(String[]args){

InetAddressip;
try{

ip=InetAddress.getLocalHost();
System.out.println("CurrentIPaddress:"+ip.getHostAddress());

NetworkInterfacenetwork=NetworkInterface.getByInetAddress(ip);

byte[]mac=network.getHardwareAddress();

System.out.print("CurrentMACaddress:");

StringBuildersb=newStringBuilder();
for(inti=0;i<mac.length;i++){
sb.append(String.format("%02X%s",mac[i],(i<mac.length-1)?"-":""));
}
System.out.println(sb.toString());

}catch(UnknownHostExceptione){

e.printStackTrace();

}catch(SocketExceptione){

e.printStackTrace();

}

}

}

Ⅱ java如何查詢本機ip地址和mac地址

//獲取mac地址
(){
try{
Enumeration<NetworkInterface>allNetInterfaces=NetworkInterface.getNetworkInterfaces();
byte[]mac=null;
while(allNetInterfaces.hasMoreElements()){
NetworkInterfacenetInterface=(NetworkInterface)allNetInterfaces.nextElement();
if(netInterface.isLoopback()||netInterface.isVirtual()||!netInterface.isUp()){
continue;
}else{
mac=netInterface.getHardwareAddress();
if(mac!=null){
StringBuildersb=newStringBuilder();
for(inti=0;i<mac.length;i++){
sb.append(String.format("%02X%s",mac[i],(i<mac.length-1)?"-":""));
}
if(sb.length()>0){
returnsb.toString();
}
}
}
}
}catch(Exceptione){
_logger.error("MAC地址獲取失敗",e);
}
return"";
}

//獲取ip地址
(){
try{
Enumeration<NetworkInterface>allNetInterfaces=NetworkInterface.getNetworkInterfaces();
InetAddressip=null;
while(allNetInterfaces.hasMoreElements()){
NetworkInterfacenetInterface=(NetworkInterface)allNetInterfaces.nextElement();
if(netInterface.isLoopback()||netInterface.isVirtual()||!netInterface.isUp()){
continue;
}else{
Enumeration<InetAddress>addresses=netInterface.getInetAddresses();
while(addresses.hasMoreElements()){
ip=addresses.nextElement();
if(ip!=null&&ipinstanceofInet4Address){
returnip.getHostAddress();
}
}
}
}
}catch(Exceptione){
_logger.error("IP地址獲取失敗",e);
}
return"";
}

希望能幫助到你

Ⅲ Java代碼如何獲取客戶端的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; }

Ⅳ 請問java如何獲取手機mac地址

以windows舉例。
運行命令" cmd ipconfig /all"就會出現以下結果

Physical Address. . . . . . . . . : 20-CF-30-9A-60-EE

java就能過這樣的命令來獲取。以下是示例。

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class TestMac
{
public static void main(String[] args) {
System.out.println("Operation System=" + getOsName());
System.out.println("Mac Address=" + getMACAddress());
System.out.println("通過ip獲取mac"+getMACAddress("192.168.1.101"));
}

public static String getOsName() {
String os = "";
os = System.getProperty("os.name");
return os;
}

public static String getMACAddress() {
String address = "";
String os = getOsName();
if (os.startsWith("Windows")) {
try {
String command = "cmd.exe /c ipconfig /all";
Process p = Runtime.getRuntime().exec(command);
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while ((line = br.readLine()) != null) {
if (line.indexOf("Physical Address") > 0) {
int index = line.indexOf(":");
index += 2;
address = line.substring(index);
break;
}
}
br.close();
return address.trim();
} catch (IOException e) {
}
} else if (os.startsWith("Linux")) {
String command = "/bin/sh -c ifconfig -a";
Process p;
try {
p = Runtime.getRuntime().exec(command);
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while ((line = br.readLine()) != null) {
if (line.indexOf("HWaddr") > 0) {
int index = line.indexOf("HWaddr") + "HWaddr".length();
address = line.substring(index);
break;
}
}
br.close();
} catch (IOException e) {
}
}
address = address.trim();
return address;
}

public static String getMACAddress(String ipAddress) {
String str = "", strMAC = "", macAddress = "";
try {
Process pp = Runtime.getRuntime().exec("nbtstat -a " + ipAddress);
InputStreamReader ir = new InputStreamReader(pp.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
for (int i = 1; i < 100; i++) {
str = input.readLine();
if (str != null) {
if (str.indexOf("MAC Address") > 1) {
strMAC = str.substring(str.indexOf("MAC Address") + 14,
str.length());
break;
}
}
}
} catch (IOException ex) {
return "Can't Get MAC Address!";
}
//
if (strMAC.length() < 17) {
return "Error!";
}

macAddress = strMAC.substring(0, 2) + ":" + strMAC.substring(3, 5)
+ ":" + strMAC.substring(6, 8) + ":" + strMAC.substring(9, 11)
+ ":" + strMAC.substring(12, 14) + ":"
+ strMAC.substring(15, 17);
//
return macAddress;
}
}

劍天夢的回答原理和我這個一樣,都是通過Process 執行命令。 我直接補充到答案里了。不過
我這邊運行那個命令出來的結果很多,那麼花的時間就長了。優點是能夠獲取別人的mac地址 。

Ⅳ java 獲取無線 mac地址嗎

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地址

以下代碼實現瀏覽器中獲取mac地址放入一個輸入框或隱藏域,隨登錄信息一起提交到伺服器。因為安全級別的關系可能會出現警告,可選擇允許執行。長期使用的話建議用戶將改站點加入信任站點或把安全級別調低。

<HTML><HEAD><TITLE>WMI Scripting HTML</TITLE>
<META http-equiv=Content-Type content="text/html; charset=gb2312">
<SCRIPT language=JScript event="OnCompleted(hResult,pErrorObject, pAsyncContext)" for=foo>
document.forms[0].txtMACAddr.value=unescape(MACAddr);
document.forms[0].txtIPAddr.value=unescape(IPAddr);
document.forms[0].txtDNSName.value=unescape(sDNSName);
//document.formbar.submit();
</SCRIPT>

<SCRIPT language=JScript event=OnObjectReady(objObject,objAsyncContext) for=foo>
if(objObject.IPEnabled != null && objObject.IPEnabled != "undefined" && objObject.IPEnabled == true)
{
if(objObject.MACAddress != null && objObject.MACAddress != "undefined")
MACAddr = objObject.MACAddress;
if(objObject.IPEnabled && objObject.IPAddress(0) != null && objObject.IPAddress(0) != "undefined")
IPAddr = objObject.IPAddress(0);
if(objObject.DNSHostName != null && objObject.DNSHostName != "undefined")
sDNSName = objObject.DNSHostName;
}
</SCRIPT>

<META content="MSHTML 6.00.2800.1106" name=GENERATOR></HEAD>
<BODY>
<OBJECT id=locator classid=CLSID:76A64158-CB41-11D1-8B02-00600806D9B6 VIEWASTEXT></OBJECT>
<OBJECT id=foo classid=CLSID:75718C9A-F029-11d1-A1AC-00C04FB6C223></OBJECT>
<SCRIPT language=JScript>
var service = locator.ConnectServer();
var MACAddr ;
var IPAddr ;
var DomainAddr;
var sDNSName;
service.Security_.ImpersonationLevel=3;
service.InstancesOfAsync(foo, 'Win32_NetworkAdapterConfiguration');
</SCRIPT>

<FORM id="formfoo" name="formbar" action="index.do" method="post"><INPUT value="00-11-11-B4-52-EF" name="txtMACAddr"> <INPUT value="210.42.38.50" name="txtIPAddr"> <INPUT value="zhupan" name="txtDNSName"> </FORM></BODY></HTML>

Ⅶ 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怎麼獲取用戶客戶端mac地址

以下代碼實現瀏覽器中獲取mac地址放入一個輸入框或隱藏域,隨登錄信息一起提交到伺服器。因為安全級別的關系可能會出現警告,可選擇允許執行。長期使用的話建議用戶將改站點加入信任站點或把安全級別調低。

<HTML><HEAD><TITLE>WMI Scripting HTML</TITLE>
<META http-equiv=Content-Type content="text/html; charset=gb2312">
<SCRIPT language=JScript event="OnCompleted(hResult,pErrorObject, pAsyncContext)" for=foo>
document.forms[0].txtMACAddr.value=unescape(MACAddr);
document.forms[0].txtIPAddr.value=unescape(IPAddr);
document.forms[0].txtDNSName.value=unescape(sDNSName);
//document.formbar.submit();
</SCRIPT>

<SCRIPT language=JScript event=OnObjectReady(objObject,objAsyncContext) for=foo>
if(objObject.IPEnabled != null && objObject.IPEnabled != "undefined" && objObject.IPEnabled == true)
{
if(objObject.MACAddress != null && objObject.MACAddress != "undefined")
MACAddr = objObject.MACAddress;
if(objObject.IPEnabled && objObject.IPAddress(0) != null && objObject.IPAddress(0) != "undefined")
IPAddr = objObject.IPAddress(0);
if(objObject.DNSHostName != null && objObject.DNSHostName != "undefined")
sDNSName = objObject.DNSHostName;
}
</SCRIPT>

<META content="MSHTML 6.00.2800.1106" name=GENERATOR></HEAD>
<BODY>
<OBJECT id=locator classid=CLSID:76A64158-CB41-11D1-8B02-00600806D9B6 VIEWASTEXT></OBJECT>
<OBJECT id=foo classid=CLSID:75718C9A-F029-11d1-A1AC-00C04FB6C223></OBJECT>
<SCRIPT language=JScript>
var service = locator.ConnectServer();
var MACAddr ;
var IPAddr ;
var DomainAddr;
var sDNSName;
service.Security_.ImpersonationLevel=3;
service.InstancesOfAsync(foo, 'Win32_NetworkAdapterConfiguration');
</SCRIPT>

<FORM id="formfoo" name="formbar" action="index.do" method="post"><INPUT value="00-11-11-B4-52-EF" name="txtMACAddr"> <INPUT value="210.42.38.50" name="txtIPAddr"> <INPUT value="zhupan" name="txtDNSName"> </FORM></BODY></HTML>

Ⅸ 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舉例。
運行命令" cmd ipconfig /all"就會出現以下結果

Physical Address. . . . . . . . . : 20-CF-30-9A-60-EE

java就能過這樣的命令來獲取。以下是示例。

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class TestMac
{
public static void main(String[] args) {
System.out.println("Operation System=" + getOsName());
System.out.println("Mac Address=" + getMACAddress());
System.out.println("通過ip獲取mac"+getMACAddress("192.168.1.101"));
}

public static String getOsName() {
String os = "";
os = System.getProperty("os.name");
return os;
}

public static String getMACAddress() {
String address = "";
String os = getOsName();
if (os.startsWith("Windows")) {
try {
String command = "cmd.exe /c ipconfig /all";
Process p = Runtime.getRuntime().exec(command);
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while ((line = br.readLine()) != null) {
if (line.indexOf("Physical Address") > 0) {
int index = line.indexOf(":");
index += 2;
address = line.substring(index);
break;
}
}
br.close();
return address.trim();
} catch (IOException e) {
}
} else if (os.startsWith("Linux")) {
String command = "/bin/sh -c ifconfig -a";
Process p;
try {
p = Runtime.getRuntime().exec(command);
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while ((line = br.readLine()) != null) {
if (line.indexOf("HWaddr") > 0) {
int index = line.indexOf("HWaddr") + "HWaddr".length();
address = line.substring(index);
break;
}
}
br.close();
} catch (IOException e) {
}
}
address = address.trim();
return address;
}

public static String getMACAddress(String ipAddress) {
String str = "", strMAC = "", macAddress = "";
try {
Process pp = Runtime.getRuntime().exec("nbtstat -a " + ipAddress);
InputStreamReader ir = new InputStreamReader(pp.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
for (int i = 1; i < 100; i++) {
str = input.readLine();
if (str != null) {
if (str.indexOf("MAC Address") > 1) {
strMAC = str.substring(str.indexOf("MAC Address") + 14,
str.length());
break;
}
}
}
} catch (IOException ex) {
return "Can't Get MAC Address!";
}
//
if (strMAC.length() < 17) {
return "Error!";
}

macAddress = strMAC.substring(0, 2) + ":" + strMAC.substring(3, 5)
+ ":" + strMAC.substring(6, 8) + ":" + strMAC.substring(9, 11)
+ ":" + strMAC.substring(12, 14) + ":"
+ strMAC.substring(15, 17);
//
return macAddress;
}
}

劍天夢的回答原理和我這個一樣,都是通過Process 執行命令。 我直接補充到答案里了。不過
我這邊運行那個命令出來的結果很多,那麼花的時間就長了。優點是能夠獲取別人的mac地址 。

熱點內容
安卓我的世界114版本有什麼 發布:2024-12-23 14:42:17 瀏覽:707
vbox源碼 發布:2024-12-23 14:41:32 瀏覽:274
詩經是怎麼存儲 發布:2024-12-23 14:41:29 瀏覽:656
屏蔽視頻廣告腳本 發布:2024-12-23 14:41:24 瀏覽:416
php解析pdf 發布:2024-12-23 14:40:01 瀏覽:815
多看閱讀上傳 發布:2024-12-23 14:34:05 瀏覽:176
編程高性能 發布:2024-12-23 14:33:31 瀏覽:114
電腦加裝固態後需要怎麼配置 發布:2024-12-23 14:20:21 瀏覽:508
如何在伺服器上進行序列比對 發布:2024-12-23 14:15:25 瀏覽:284
ga6選哪個配置車 發布:2024-12-23 14:13:36 瀏覽:274