获取本地IP地址而无需连接到互联网

所以,我试图让我的机器的IP地址在我的本地networking(应该是192.168.178.41 )。

我的第一个意图是使用这样的东西:

 InetAddress.getLocalHost().getHostAddress(); 

但它只返回127.0.0.1 ,这是正确的,但对我来说不是很有帮助。

我周围search,发现这个答案https://stackoverflow.com/a/2381398/717341 ,它只是创build一个连接到一些网页(例如“google.com”)的Socket连接,并从套接字获取本地主机地址:

 Socket s = new Socket("google.com", 80); System.out.println(s.getLocalAddress().getHostAddress()); s.close(); 

这对我的机器(它返回192.168.178.41 ),但它需要连接到互联网才能正常工作。 由于我的应用程序不需要互联网连接,它可能看起来应该试图连接到谷歌每次启动“可疑”,我不喜欢使用它的想法。

所以,经过一些更多的研究,我偶然发现了NetworkInterface类,它(有些工作)也返回了所需的IP地址:

 Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()){ NetworkInterface current = interfaces.nextElement(); System.out.println(current); if (!current.isUp() || current.isLoopback() || current.isVirtual()) continue; Enumeration<InetAddress> addresses = current.getInetAddresses(); while (addresses.hasMoreElements()){ InetAddress current_addr = addresses.nextElement(); if (current_addr.isLoopbackAddress()) continue; System.out.println(current_addr.getHostAddress()); } } 

在我的机器上,这返回以下内容:

 name:eth1 (eth1) fe80:0:0:0:226:4aff:fe0d:592e%3 192.168.178.41 name:lo (lo) 

它find我的networking接口,并返回所需的IP,但我不知道其他地址( fe80:0:0:0:226:4aff:fe0d:592e%3 )是什么意思。

此外,我还没有find一种方法来过滤从返回的地址(通过使用InetAddressisXX()方法isXX()isXX() ,使用正则expression式,我觉得非常“脏”。

任何其他的想法,而不是使用正则expression式或互联网?

fe80:0:0:0:226:4aff:fe0d:592e是你的ipv6地址;-)。

使用这个检查

 if (current_addr instanceof Inet4Address) System.out.println(current_addr.getHostAddress()); else if (current_addr instanceof Inet6Address) System.out.println(current_addr.getHostAddress()); 

如果你只关心IPv4,那么就放弃IPv6的情况。 但要小心,IPv6是未来^^。

PS:检查一下你的break是否应该continue

这里也是一个java 8的方法:

 public static String getIp() throws SocketException { return Collections.list(NetworkInterface.getNetworkInterfaces()).stream() .flatMap(i -> Collections.list(i.getInetAddresses()).stream()) .filter(ip -> ip instanceof Inet4Address && ip.isSiteLocalAddress()) .findFirst().orElseThrow(RuntimeException::new) .getHostAddress(); } 
 public static String getIp(){ String ipAddress = null; Enumeration<NetworkInterface> net = null; try { net = NetworkInterface.getNetworkInterfaces(); } catch (SocketException e) { throw new RuntimeException(e); } while(net.hasMoreElements()){ NetworkInterface element = net.nextElement(); Enumeration<InetAddress> addresses = element.getInetAddresses(); while (addresses.hasMoreElements()){ InetAddress ip = addresses.nextElement(); if (ip instanceof Inet4Address){ if (ip.isSiteLocalAddress()){ ipAddress = ip.getHostAddress(); } } } } return ipAddress; } 
 import java.net.*; public class Get_IP { public static void main(String args[]) { try { InetAddress addr = InetAddress.getLocalHost(); String hostname = addr.getHostName(); System.out.println(addr.getHostAddress()); System.out.println(hostname); }catch(UnknownHostException e) { //throw Exception } } 

}

扬基的答案对于第一部分是正确的。 要打印出IP地址,你可以将它作为一个字节数组,并将其转换为普通的string表示forms,如下所示:

 StringBuilder ip = new StringBuilder(); for(byte b : current_addr.getAddress()) { // The & here makes b convert like an unsigned byte - so, 255 instead of -1. ip.append(b & 0xFF).append('.'); } ip.setLength(ip.length() - 1); // To remove the last '.' System.out.println(ip.toString());