public void getIpAddr() {
Enumeration allNetInterfaces = null;
try {
allNetInterfaces = NetworkInterface.getNetworkInterfaces();
} catch (SocketException e) {
e.printStackTrace();
}
InetAddress ip = null;
while (allNetInterfaces.hasMoreElements()) {
NetworkInterface netInterface = (NetworkInterface) allNetInterfaces
.nextElement();
System.out.println(netInterface.getName());
Enumeration addresses = netInterface.getInetAddresses();
while (addresses.hasMoreElements()) {
ip = (InetAddress) addresses.nextElement();
if (ip != null && ip instanceof Inet4Address
&& ip.getHostAddress().indexOf(".") != -1) {
System.out.println("本机的IP = " + ip.getHostAddress());
logger.debug("------------------本机的IP : "+ip.getHostAddress()+"---------------------");
}
}
}
}
linux/Windows中JAVA获取IP地址
最新推荐文章于 2023-02-03 13:44:22 发布