java 如何提取本地IP
2024-11-28 广告
《java》中提取本地IP的方法如下:
private static void getIpAddressByNetworkInterface() {
try {
Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
NetworkInterface net;
InetAddress inetAddress;
while (nets.hasMoreElements()) {
net = nets.nextElement();
Enumeration<InetAddress> address = net.getInetAddresses();
while (address.hasMoreElements()) {
inetAddress = address.nextElement();
if (inetAddress!=null&&inetAddress instanceof Inet4Address)
System.out.println(inetAddress.getHostAddress());
}
}
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
参考资料: 有问题,给我发百度消息