Java Get Client MAC Address
In Java/JSP, there are three methods available for obtaining the MAC address of a client’s network card.
One common way to retrieve a MAC address is by using the NetworkInterface class in Java. This can be done by calling the getHardwareAddress() method of the NetworkInterface class. Here is an example code snippet:
import java.net.NetworkInterface;
import java.net.SocketException;
public class GetMacAddress {
public static void main(String[] args) {
try {
// 获取本地网络接口对象
NetworkInterface networkInterface = NetworkInterface.getByName("eth0");
// 获取MAC地址
byte[] mac = networkInterface.getHardwareAddress();
// 将MAC地址转换为十六进制字符串
StringBuilder macAddress = new StringBuilder();
for (int i = 0; i < mac.length; i++) {
macAddress.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
}
System.out.println("MAC地址:" + macAddress.toString());
} catch (SocketException e) {
e.printStackTrace();
}
}
}
To obtain the MAC address, you can utilize the InetAddress class in Java. You can start by calling the getLocalHost() method to get the InetAddress object of the local host. Then, you can use the getHostName() method to retrieve the host name, followed by calling the getByName() method to get the InetAddress object. Lastly, you can use the getHardwareAddress() method to retrieve the MAC address. Here is an example code:
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
public class GetMacAddress {
public static void main(String[] args) {
try {
// 获取本地主机InetAddress对象
InetAddress localHost = InetAddress.getLocalHost();
// 获取主机名
String hostname = localHost.getHostName();
// 获取本地网络接口对象
NetworkInterface networkInterface = NetworkInterface.getByName(hostname);
// 获取MAC地址
byte[] mac = networkInterface.getHardwareAddress();
// 将MAC地址转换为十六进制字符串
StringBuilder macAddress = new StringBuilder();
for (int i = 0; i < mac.length; i++) {
macAddress.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
}
System.out.println("MAC地址:" + macAddress.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
}
3. Utilizing JavaScript and JSP: JavaScript can be used to retrieve the MAC address on the client side, and then pass it to a JSP page through Ajax. Here is an example code:
<!DOCTYPE html>
<html>
<head>
<title>Get MAC Address</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
function getMacAddress() {
var macAddress = null;
new ActiveXObject("WScript.Shell").Run("cmd /k getmac /v", 0, true);
var wmi = new ActiveXObject("WbemScripting.SWbemLocator");
var service = wmi.ConnectServer(".");
var properties = service.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True");
var e = new Enumerator(properties);
for (; !e.atEnd(); e.moveNext()) {
var p = e.item();
macAddress = p.MACAddress;
break;
}
return macAddress;
}
$(document).ready(function() {
var macAddress = getMacAddress();
$.ajax({
url: "getMacAddress.jsp",
type: "POST",
data: {macAddress: macAddress},
success: function(response) {
console.log(response);
}
});
});
</script>
</head>
<body>
</body>
</html>
In getMacAddress.jsp, you can use request.getParameter(“macAddress”) to retrieve the MAC address parameter.