1???Java?NetworkInterface?:????????,????NetworkInterface??getHardwareAddress()?????MAC????????????:
```java
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("X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
}
System.out.println("MAC??:" + macAddress.toString());
} catch (SocketException e) {
e.printStackTrace();
}
}
}
```
2???Java?InetAddress?:????InetAddress??getLocalHost()?????????InetAddress??,????getHostName()???????,???getByName()????InetAddress??,????getHardwareAddress()????MAC????????????:
```java
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("X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
}
System.out.println("MAC??:" + macAddress.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
3???JavaScript?JSP:????JavaScript??????MAC??,??????Ajax???JSP????????????:
```html
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);
}
});
});
```
?getMacAddress.jsp?,????request.getParameter("macAddress")???MAC?????