import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;
public class IPUtils {
private static final Logger LOGGER = LoggerFactory.getLogger(IPUtils.class);
public static String getIp() {
String ip = getLocalIp();
if (ip == null || "".equals(ip)) {
ip = getLocalIp2();
}
return ip;
}
private static String getLocalIp() {
try {
Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
while (en.hasMoreElements()) {
NetworkInterface networkInterface = en.nextElement();
Enumeration<InetAddress> addresses = networkInterface.getInetAddresses();
while (addresses.hasMoreElements()) {
InetAddress address = addresses.nextElement();
if (!address.isLoopbackAddress() && !address.isLinkLocalAddress() && !address.isSiteLocalAddress()) {
String ipAddress = address.getHostAddress();
LOGGER.info("获取到的IP为【{}】", ipAddress);
return ipAddress;
}
}
}
} catch (Exception e) {
LOGGER.error("getLocalIP1 获取IP报错", e);
}
return null;
}
public static String getLocalIp2() {
try {
return InetAddress.getLocalHost().getHostAddress();
} catch (Exception e) {
LOGGER.error("getLocalIp2 获取IP报错", e);
}
return null;
}
}
【Java】获取IP地址的工具类
最新推荐文章于 2024-07-29 14:22:26 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)