以下方法,调用一下,你就明白了。
public static void showNetworkInfo() {
// 获取本地所有网络接口
Enumeration<NetworkInterface> nifs = null;
try {
nifs = NetworkInterface.getNetworkInterfaces();
} catch (SocketException e) {
e.printStackTrace();
}
while (nifs.hasMoreElements()) {
NetworkInterface ni = nifs.nextElement();
Enumeration<InetAddress> address = ni.getInetAddresses();
while (address.hasMoreElements()) {
InetAddress addr = address.nextElement();
if (addr instanceof Inet4Address) {
System.out.println("网络接口名称为:" + ni.getName());
System.out.println("网卡接口地址:" + addr.getHostAddress());
System.out.println();
}
}
}
}