当前位置:首页 » 编程语言 » 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地址 。

热点内容
安卓上哪里下大型游戏 发布:2024-12-23 15:10:58 浏览:186
明日之后目前适用于什么配置 发布:2024-12-23 14:56:09 浏览:51
php全角半角 发布:2024-12-23 14:55:17 浏览:826
手机上传助手 发布:2024-12-23 14:55:14 浏览:730
什么样的主机配置吃鸡开全效 发布:2024-12-23 14:55:13 浏览:828
安卓我的世界114版本有什么 发布:2024-12-23 14:42:17 浏览:708
vbox源码 发布:2024-12-23 14:41:32 浏览:275
诗经是怎么存储 发布:2024-12-23 14:41:29 浏览:657
屏蔽视频广告脚本 发布:2024-12-23 14:41:24 浏览:417
php解析pdf 发布:2024-12-23 14:40:01 浏览:816