方法一
public
static void main(String[] args)
throws Exception {
Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
while
(networkInterfaces.hasMoreElements()) {
NetworkInterface ni = networkInterfaces.nextElement();
Enumeration<InetAddress> inetAddresses = ni.getInetAddresses();
while (inetAddresses.hasMoreElements()) {
InetAddress inetAddress = inetAddresses.nextElement();
System.out.println(inetAddress.getHostAddress());
}
}
}
方法二
InetAddress ip = InetAddress.getLocalHost();
NetworkInterface network = NetworkInterface.getByInetAddress(ip);
byte[] mac = network.getHardwareAddress();
System.out.print("Current 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());