网络 - 在J中获取“外部”IP地址
我不太确定如何获取机器的外部IP地址,因为网络外的计算机会看到它。
我的以下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;
}
}
11个解决方案
149 votes
我不确定您是否可以从本地计算机上运行的代码中获取该IP。
但是,您可以构建在网站上运行的代码,例如在JSP中运行,然后使用返回请求来源的IP的内容:
request.getRemoteAddr()
或者只是使用已经存在的服务来执行此操作,然后解析服务中的答案以找出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);
bakkal answered 2019-04-26T09:30:19Z
70 votes
@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();
}
}
}
}
}
Will answered 2019-04-26T09:30:51Z
15 votes
事实是,“你不能”,因为你提出了这个问题。 NAT发生在协议之外。 您的计算机内核无法知道您的NAT盒如何从外部IP地址映射到内部IP地址。 这里的其他答案提供了涉及与外部网站交谈的方法的技巧。
bmargulies answered 2019-04-26T09:31:16Z
9 votes
正如@Donal Fellows所写,你必须查询网络接口而不是机器。 来自javadocs的代码对我有用:
以下示例程序列出了计算机上的所有网络接口及其地址:
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 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 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
domih answered 2019-04-26T09:32:01Z
9 votes
所有这些仍然运行顺利! (截至2016年12月19日)
[http://checkip.amazonaws.com/]
[http://icanhazip.com/]
[http://www.trackip.net/ip]
[http://myexternalip.com/raw]
[http://ipecho.net/plain]
[http://curlmyip.com/](2016年12月17日)
[http://bot.whatismyipaddress.com]
建议:不要直接依赖其中一个; 尝试使用一个,但有一个考虑他人的应急计划! 你使用的越多越好!
祝好运!
FranciscoBouza answered 2019-04-26T09:33:23Z
5 votes
对像www.whatismyip.com这样的网站进行HttpURLConnection并解析:-)
nc3b answered 2019-04-26T09:33:48Z
4 votes
[http://jstun.javawi.de/]会这样做 - 只要您的网关设备执行STUN)大多数都会这样做)
user461467 answered 2019-04-26T09:34:13Z
4 votes
这个怎么样? 它很简单,对我来说效果最好:)
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();
}
}
}
Sparkoenig answered 2019-04-26T09:34:38Z
1 votes
这并不容易,因为局域网内的机器通常不关心其路由器的外部IP到互联网......它根本不需要它!
我建议你通过打开像[http://www.whatismyip.com/]这样的网站并通过解析html结果来获取IP号码来利用它。它应该不那么难!
Jack answered 2019-04-26T09:35:09Z
0 votes
如果您正在使用基于JAVA的webapp,并且想要获取客户端(通过浏览器发出请求的人),则外部ip尝试在公共域中部署应用程序并使用request.getRemoteAddr()来读取外部IP地址。
Jay answered 2019-04-26T09:35:34Z
0 votes
System.out.println(pageCrawling.getHtmlFromURL("http://ipecho.net/plain"));
Mohsen Abasi answered 2019-04-26T09:35:52Z