Jul*_*lio 81 java networking network-programming external ip-address
我不太确定如何获取机器的外部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;
}
}
Run Code Online (Sandbox Code Playgroud)
bak*_*kal 161
我不确定您是否可以从本地计算机上运行的代码中获取该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);
Run Code Online (Sandbox Code Playgroud)
Wil*_*ill 75
@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();
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
bma*_*ies 17
事实是,"你不能",因为你提出了这个问题.NAT发生在协议之外.您的计算机内核无法知道您的NAT盒如何从外部IP地址映射到内部IP地址.这里的其他答案提供了涉及与外部网站交谈的方法的技巧.
dom*_*mih 11
正如@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<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");
}
}
Run Code Online (Sandbox Code Playgroud)
以下是示例程序的示例输出:
Display name: TCP Loopback interface
Name: lo
InetAddress: /127.0.0.1
Display name: Wireless Network Connection
Name: eth0
InetAddress: /192.0.2.0
Run Code Online (Sandbox Code Playgroud)
Fra*_*uza 11
所有这些仍然运行顺利!(截至2016年12月19日)
建议:不要直接依赖其中一个; 尝试使用一个,但考虑到其他人有一个contigency计划!你使用的越多越好!
祝好运!
小智 5
这个怎么样?这很简单,对我来说效果最好:)
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();
}
}
}
Run Code Online (Sandbox Code Playgroud)