Java は MAC OS ネットワーク カードの MAC アドレスを取得します
package com.macro.mall.portal.util;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
public class mac {
public static void main(String[] args) {
try {
Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
while (networkInterfaces.hasMoreElements()) {
NetworkInterface networkInterface = networkInterfaces.nextElement();
byte[] macAddress = networkInterface.getHardwareAddress();
if (macAddress != null) {
System.out.print("MAC address for " + networkInterface.getName() + ": ");
for (int i = 0; i < macAddress.length; i++) {
System.out.format("%02X%s", macAddress[i], (i < macAddress.length - 1) ? "-" : "");
}
System.out.println();
}
}
} catch (SocketException e) {
e.printStackTrace();
}
}
}
テスト結果:直接リクエストで取得したデータ+ネットワークリクエストを変更して取得したデータは同じ シャットダウン、再起動後:llw0のMACアドレス、awdl0のMACアドレスが変化 コンピュータに常に同じデータを取得させたい場合、判定を追加して出力できます
if ("en0".equals(nic.getName())) {
}