java获取服务的局域网ip
解决方法:
private static String getLocalHostLANAddress() {
try {
InetAddress candidateAddress = null;
Enumeration ifaces = NetworkInterface.getNetworkInterfaces();
while(ifaces.hasMoreElements()) {
NetworkInterface iface = (NetworkInterface)ifaces.nextElement();
Enumeration inetAddrs = iface.getInetAddresses();
while(inetAddrs.hasMoreElements()) {
InetAddress inetAddr = (InetAddress)inetAddrs.nextElement();
if (!inetAddr.isLoopbackAddress()) {
if (inetAddr.isSiteLocalAddress()) {
return inetAddr;
}
if (candidateAddress == null) {
candidateAddress = inetAddr;
}
}
}
}
if (candidateAddress != null) {
return candidateAddress;
} else {
InetAddress jdkSuppliedAddress = InetAddress.getLocalHost();
return jdkSuppliedAddress.getHostName();
}
} catch (Exception var5) {
return null;
}
}