getLocalHost()在linux中抛出unknownHostException,直到我在/etc/hosts中手动添加条目。有没有办法不在/etc/host文件中添加条目而获取InetAddress对象。。注意:IP是静态的
最佳答案
String host = null;
NetworkInterface iface = null;
for(Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces();ifaces.hasMoreElements();){
iface = (NetworkInterface)ifaces.nextElement();
InetAddress ia = null;
for(Enumeration<InetAddress> ips = iface.getInetAddresses();ips.hasMoreElements();){
ia = (InetAddress)ips.nextElement();
if(!ia.isLoopbackAddress() && (!ia.isLinkLocalAddress()) && (ia instanceof Inet4Address)) host=ia.getHostAddress();
}
}
关于java - InetAddress.getLocalHost()在Linux中引发unknownHostException,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3280183/