java中怎么获取电脑的mac地址

java中怎么获取电脑的mac地址,第1张

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());

}

}

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; }

起因是某个同事接到了领导安排下来的一个需求,要在一个Web应用(Java+Tomcat)中,记录用户登录时的IP地址和MAC地址,用于安全审计,于是咨询我如何实现。

第一反应是,这个需求本身是不成立的,根据以往的了解,MAC地址应该是过不了路由器的才对。

以往做开发,都是用engineer的思维:先动手做,遇到问题再解决问题。但这个需求,应当用scientist的思维去思考:首先确定能不能做,然后才是怎么做。

翻查了一些资料,想来证实" 为什么WEB服务器,可以获取到客户端的IP地址,但获取不到MAC地址 ",看着看着才发现,这是个挺大的命题,够写一篇BLOG了。

PS:由于个人对这块内容了解的不够彻底, 本文很可能会有谬误 ,请读者先不要太当真,另外希望平台组的同事给予指证。

我所认为的结论应该是这样的:

下面一步步解释一下。

先从>

import javanetInetAddress;

import javanetNetworkInterface;

public class TestOne {

public static void main(String[] arguments) throws Exception {

InetAddress ia = InetAddressgetLocalHost();// 获取本地IP对象

Systemoutprintln("MAC " + getMACAddress(ia));

}

// 获取MAC地址的方法

private static String getMACAddress(InetAddress ia) throws Exception {

// 获得网络接口对象(即网卡),并得到mac地址,mac地址存在于一个byte数组中。

byte[] mac = NetworkInterfacegetByInetAddress(ia)getHardwareAddress();

// 下面代码是把mac地址拼装成String

StringBuffer sb = new StringBuffer();

for (int i = 0; i < maclength; i++) {

if (i != 0) {

sbappend("-");

}

// mac[i] & 0xFF 是为了把byte转化为正整数

String s = IntegertoHexString(mac[i] & 0xFF);

sbappend(slength() == 1 0 + s : s);

}

// 把字符串所有小写字母改为大写成为正规的mac地址并返回

return sbtoString()toUpperCase();

}

}

读取ipconfig/all里面的内容:

public static String checkPhysicalAddress() {

String physicalAddress ="";

try {

String line;

Process process = RuntimegetRuntime()exec("cmd /c ipconfig /all");

BufferedReader bufferedReader = new BufferedReader(

new InputStreamReader(processgetInputStream()));

int temp=1;

int switchon=0;

while ((line = bufferedReaderreadLine()) != null) {

if(switchon ==1){

temp++;

}

if(lineindexOf("以太网适配器 本地连接:") !=-1){

switchon=1;

continue;

}

if (temp == 5) {

line = bufferedReaderreadLine();

Systemoutprintln("1:"+line);

if (lineindexOf(":") != -1) {

physicalAddress = linesubstring(lineindexOf(":") + 2)replaceAll("-", "")trim();

break; //找到MAC,推出循环

}

}

}

//processwaitFor();

} catch (Exception e) {

eprintStackTrace();

}

return physicalAddress;

}

一行一行的读取命令行的东西,直到我们想要的一行

如下图我们需要的是“以太网适配器 本地链接:”这里面的物理地址,上面的代码就是找到这一行,然后再下去五行就是我们要的MAC地址

以上就是关于java中怎么获取电脑的mac地址全部的内容,包括:java中怎么获取电脑的mac地址、Java代码如何获取客户端的MAC地址、WEB服务器为什么取不到用户的MAC地址等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9653265.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-30
下一篇2023-04-30

发表评论

登录后才能评论

评论列表(0条)

    保存