
代码如下:
import javanetInetAddress;
import javanetNetworkInterface;
import javanetSocketException;
import javanetUnknownHostException;
/
物理地址是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/1222067383
InetAddress ia = InetAddressgetLocalHost();
Systemoutprintln(ia);
getLocalMac(ia);
}
private static void getLocalMac(InetAddress ia) throws SocketException {
// TODO Auto-generated method stub
//获取网卡,获取地址
byte[] mac = NetworkInterfacegetByInetAddress(ia)getHardwareAddress();
Systemoutprintln("mac数组长度:"+maclength);
StringBuffer sb = new StringBuffer("");
for(int i=0; i<maclength; i++) {
if(i!=0) {
sbappend("-");
}
//字节转换为整数
int temp = mac[i]&0xff;
String str = IntegertoHexString(temp);
Systemoutprintln("每8位:"+str);
if(strlength()==1) {
sbappend("0"+str);
}else {
sbappend(str);
}
}
Systemoutprintln("本机MAC地址:"+sbtoString()toUpperCase());
}
}
通过ip获取指定ip地址的mac地址,ip可以通过请求request获取,
requestgetRemoteAddr();
(当然获取ip也不是在任何情况下都有效的)
通过了Apache,Squid等反向代理软件就不能获取到客户端的真实IP地址。
您也许需要通过其他的方式获取,(见附)
//获取mac如下 (nbtstat -A IPAddress是对给定的IP地址解析其主机名。如果不能正常解析它的主机
//名的话,有可能是防火墙屏蔽了。也可能是在DNS中将NetBios 解析选项屏蔽了。)
public String getMACAddress(String ip){
String str = "";
String macAddress = "";
try {
Process p = RuntimegetRuntime()exec("nbtstat -A " + ip);
InputStreamReader ir = new InputStreamReader(pgetInputStream());
LineNumberReader input = new LineNumberReader(ir);
for (int i = 1; i < 100; i++) {
str = inputreadLine();
if (str != null) {
if (strindexOf("MAC Address") > 1) {
macAddress = strsubstring(strindexOf("MAC Address") + 14, strlength());
break;
}
if (strindexOf("MAC Address") > 1) {
macAddress = strsubstring(strindexOf("MAC 地址") + 14, strlength());
break;
}
//以上有个判断,不同系统cmd命令执行的返回结果展示方式不一样,我测试的win7是MAC 地址
//所以又第二个if判断 你可先在你机器上cmd测试下nbtstat -A 命令 当然得有一个你可以ping通的
//网络ip地址,然后根据你得到的结果中mac地址显示方式来确定这个循环取值
}
}
} catch (IOException e) {
eprintStackTrace(Systemout);
}
return macAddress;
}
附:
通过代理了的客户端ip地址获取方式
于是可得出获得客户端真实IP地址的方法一:
public String getRemortIP(>
通过设备开通WiFi连接获取Mac地址是最可取的,代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/
设备开通WiFi连接,通过wifiManager获取Mac地址
@author 高焕杰
/
public static String getMacFromWifi(Context context){
ConnectivityManager connectivityManager = (ConnectivityManager) contextgetSystemService(ContextCONNECTIVITY_SERVICE);
State wifiState = connectivityManagergetNetworkInfo(ConnectivityManagerTYPE_WIFI)getState();
if(wifiState == NetworkInfoStateCONNECTED){//判断当前是否使用wifi连接
WifiManager wifiManager = (WifiManager) contextgetSystemService(ContextWIFI_SERVICE);
if (!wifiManagerisWifiEnabled()) { //如果当前wifi不可用
wifiManagersetWifiEnabled(true);
}
WifiInfo wifiInfo = wifiManagergetConnectionInfo();
return wifiInfogetMacAddress();
}
return null;
}
除了上面这种方法,网上还给出了另外两种方法:
1、通过调用Linux的busybox命令获取Mac地址:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/
通过调用Linux的busybox命令获取Mac地址
@author 高焕杰
/
private static String getMacFromCallCmd(){
try {
String readLine = ;
Process process = RuntimegetRuntime()exec(busybox ifconfig);
BufferedReader bufferedReader = new BufferedReader (new InputStreamReader(processgetInputStream()));
while ((readLine = bufferedReaderreadLine ()) != null) {//执行命令cmd,只取结果中含有HWaddr的这一行
if(readLinecontains(HWaddr)){
return readLinesubstring(readLineindexOf(HWaddr)+6, readLinelength()-1);
}
}
}catch(Exception e) { //如果因设备不支持busybox工具而发生异常。
eprintStackTrace();
}
return null;
}
注意:这种方法在Android Pad中可以准确获取到的Mac地址,但是在Android手机中无法准确获取到。
2、通过查询记录了MAC地址的文件(文件路径:“/proc/net/arp”)获取Mac地址:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/
通过查询记录了MAC地址的文件(文件路径:“/proc/net/arp”)获取Mac地址
@author 高焕杰
/
private static String getMacFromFile(Context context){
String readLine =;
BufferedReader bufferedReader = null;
try {
bufferedReader = new BufferedReader(new FileReader(new File(/proc/net/arp)));
int rollIndex = 0;
while((readLine = bufferedReaderreadLine())!=null){
if(rollIndex == 1){
break;
}
rollIndex = rollIndex + 1;
}
} catch (IOException e) {
eprintStackTrace();
} finally {
if (bufferedReader != null) {
try {
bufferedReaderclose();
} catch (IOException e) {
eprintStackTrace();
}
}
}
if(readLine !=null && readLinelength()>1){
String[] subReadLineArray = readLinesplit( );
int rollIndex = 1;
for(int i = 0; i < subReadLineArraylength; ++i){
if(!TextUtilsisEmpty(subReadLineArray[i])){
if(rollIndex == 4){
return subReadLineArray[i];
}
rollIndex = rollIndex + 1;
}
}
}
return null;
}
注意:无论在Android Pad中还是在Android手机中,这种方法都无法准确获取到Mac地址。
指定IP的MAC
代码如下:
Java
code
Systemoutprintln("1921681187对应网卡的MAC是:");NetworkInterface
ne=NetworkInterfacegetByInetAddress(InetAddressgetByName("1921681187"));byte[]mac=negetHardwareAddress();String
mac_s=hexByte(mac[0])+":"+hexByte(mac[1])+":"+
hexByte(mac[2])+":"+hexByte(mac[3])+":"+
hexByte(mac[4])+":"+hexByte(mac[5]);Systemoutprintln(mac_s);
程序运行结果:
1921681187对应网卡的MAC是:
00:0c:f1:20:75:58
工作组和
计算机
名字类似,可以到库里找
public String getMAC() { String mac = null; try { Process pro = RuntimegetRuntime()exec("cmdexe /c ipconfig/all"); InputStream is = progetInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String message = brreadLine(); int index = -1; while (message != null) { if ((index = messageindexOf("Physical Address")) > 0) { mac = messagesubstring(index + 36)trim(); break; } message = brreadLine(); } Systemoutprintln(mac); brclose(); prodestroy(); } catch (IOException e) { Systemoutprintln("Can't get mac address!"); return null; } return mac; }
以下代码实现浏览器中获取mac地址放入一个输入框或隐藏域,随登录信息一起提交到服务器。因为安全级别的关系可能会出现警告,可选择允许执行。长期使用的话建议用户将改站点加入信任站点或把安全级别调低。
<HTML><HEAD><TITLE>WMI Scripting HTML</TITLE>
<META >
以下代码实现浏览器中获取mac地址放入一个输入框或隐藏域,随登录信息一起提交到服务器。因为安全级别的关系可能会出现警告,可选择允许执行。长期使用的话建议用户将改站点加入信任站点或把安全级别调低。
<HTML><HEAD><TITLE>WMI Scripting HTML</TITLE>
<META >
以上就是关于Java web 怎么得到客户端的Mac地址全部的内容,包括:Java web 怎么得到客户端的Mac地址、求获取客户端mac地址java代码,急需急需,麻烦了、如何使用Java代码获取Android和ios移动终端Mac地址等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)