java自动获取电脑ip和MAC地址
利用getLocalHost获得计算机名称和ip
getByInetAddress可以确定一个IP地址属于哪一个网络接口,这个IP地址通过命令行参数传入
用getHardwareAddress方法得到物理地址
public class MAC {
public static void main(String[] args) throws IOException, DocumentException{
InetAddress ip = InetAddress.getLocalHost();
System.out.println("IP:"+ip);
NetworkInterface network = NetworkInterface.getByInetAddress(ip);
byte[] mac = network.getHardwareAddress();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < mac.length; i++) {
sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
}
String hostMac=sb.toString();
System.out.println("Current MAC address : "+hostMac);
}
}