java如何获取客户端的mac地址
时间: 2023-05-18 17:07:28 浏览: 91
Java可以通过使用NetworkInterface类来获取客户端的MAC地址。以下是获取MAC地址的示例代码:
```
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
public class GetMacAddress {
public static void main(String[] args) {
try {
InetAddress ip = InetAddress.getLocalHost();
NetworkInterface network = NetworkInterface.getByInetAddress(ip);
byte[] mac = network.getHardwareAddress();
System.out.print("MAC address : ");
StringBuilder sb = new StringBuilder();
for (int i = 0; i < mac.length; i++) {
sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
}
System.out.println(sb.toString());
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (SocketException e){
e.printStackTrace();
}
}
}
```
这段代码将输出客户端的MAC地址。
阅读全文