在Java中获得“外部”IP地址

我不太清楚如何获取机器的外部IP地址,因为networking外的计算机会看到它。

我的下面的IPAddress类只获取本机的本地IP地址。

public class IPAddress { private InetAddress thisIp; private String thisIpAddress; private void setIpAdd() { try { InetAddress thisIp = InetAddress.getLocalHost(); thisIpAddress = thisIp.getHostAddress().toString(); } catch (Exception e) { } } protected String getIpAddress() { setIpAdd(); return thisIpAddress; } } 

我不确定是否可以从本地计算机上运行的代码获取IP。

但是,您可以构build在JSP中运行的代码,然后使用返回请求来源IP的内容:

request.getRemoteAddr()

或者简单地使用已经存在的服务来parsing,然后parsing服务的答案来找出IP。

使用AWS等Web服务

 import java.net.*; import java.io.*; URL whatismyip = new URL("http://checkip.amazonaws.com"); BufferedReader in = new BufferedReader(new InputStreamReader( whatismyip.openStream())); String ip = in.readLine(); //you get the IP as a String System.out.println(ip); 

@stivlo的评论之一应该是一个答案:

您可以使用亚马逊服务http://checkip.amazonaws.com

 import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.URL; public class IpChecker { public static String getIp() throws Exception { URL whatismyip = new URL("http://checkip.amazonaws.com"); BufferedReader in = null; try { in = new BufferedReader(new InputStreamReader( whatismyip.openStream())); String ip = in.readLine(); return ip; } finally { if (in != null) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } } } 

事实是,'你不能',就是你提出这个问题。 NAT在协议之外发生。 你的机器内核没有办法知道你的NAT盒子是如何从外部映射到内部IP地址的。 这里的其他答案提供涉及与外部网站交谈方法的技巧。

正如@Donal Fellows写道,你必须查询networking接口而不是机器。 这个来自javadocs的代码为我工作:

以下示例程序列出了计算机上的所有networking接口及其地址:

 import java.io.*; import java.net.*; import java.util.*; import static java.lang.System.out; public class ListNets { public static void main(String args[]) throws SocketException { Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces(); for (NetworkInterface netint : Collections.list(nets)) displayInterfaceInformation(netint); } static void displayInterfaceInformation(NetworkInterface netint) throws SocketException { out.printf("Display name: %s\n", netint.getDisplayName()); out.printf("Name: %s\n", netint.getName()); Enumeration<InetAddress> inetAddresses = netint.getInetAddresses(); for (InetAddress inetAddress : Collections.list(inetAddresses)) { out.printf("InetAddress: %s\n", inetAddress); } out.printf("\n"); } } 

以下是示例程序的示例输出:

 Display name: TCP Loopback interface Name: lo InetAddress: /127.0.0.1 Display name: Wireless Network Connection Name: eth0 InetAddress: /192.0.2.0 

从docs.oracle.com

这一切都还在起作用! (截至2016年12月19日)

build议:不要直接依靠其中的一个; 尝试使用一个,但有一个考虑他人的一个应急计划! 你用的越多越好!

祝你好运!

做一个HttpURLConnection像www.whatismyip.com一些网站,并parsing:-)

http://jstun.javawi.de/将做到这一点; – 只要你的网关设备确实STUN)最)

这个怎么样? 这很简单,对我来说最好:)

 import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.MalformedURLException; import java.net.URL; public class IP { public static void main(String args[]) { new IP(); } public IP() { URL ipAdress; try { ipAdress = new URL("http://myexternalip.com/raw"); BufferedReader in = new BufferedReader(new InputStreamReader(ipAdress.openStream())); String ip = in.readLine(); System.out.println(ip); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } 

这并不容易,因为局域网内的一台机器通常不关心其路由器的外部IP到互联网。它根本就不需要它!

我build议你通过打开一个像http://www.whatismyip.com/这样的网站,并通过parsingHTML结果来得到IP号码来利用这个。它不应该那么难!;

如果您使用的是基于JAVA的webapp,并且想要抓取客户端(通过浏览器发出请求的人)外部ip,请尝试将应用程序部署到公共领域,并使用request.getRemoteAddr()读取外部IP地址。

 System.out.println(pageCrawling.getHtmlFromURL("http://ipecho.net/plain"));