在Java中获取“外部” 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;
    }
}
回答:
我不确定是否可以从在本地计算机上运行的代码中获取该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);
以上是 在Java中获取“外部” IP地址 的全部内容, 来源链接: utcz.com/qa/420704.html








