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

IT小君   2021-09-15T00:23:33

我不太确定如何获取机器的外部 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;
    }
}
点击广告,支持我们为你提供更好的服务
评论(12)
IT小君

我不确定您是否可以从本地机器上运行的代码中获取该 IP。

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

request.getRemoteAddr()

或者简单地使用已经存在的服务来执行此操作,然后解析来自服务的答案以找出 IP。

使用 AWS 等网络服务

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);
2021-09-15T00:23:35   回复
IT小君

@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();
                }
            }
        }
    }
}
2021-09-15T00:23:36   回复
IT小君

事实是:从你提出问题的意义上说,“你不能”。NAT 发生在协议之外。您机器的内核无法知道您的 NAT 盒如何从外部 IP 地址映射到内部 IP 地址。这里的其他答案提供了涉及与外部网站交谈的方法的技巧。

2021-09-15T00:23:36   回复
IT小君

所有这些都还在正常运行!(截至 2019 年 9 月 23 日)

忠告:不要直接依赖其中之一;尝试使用一个,但有一个考虑到其他人的应急计划!使用越多越好!

祝你好运!

2021-09-15T00:23:36   回复
IT小君

正如@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");
     }
} 

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

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

2021-09-15T00:23:37   回复
IT小君

与 www.whatismyip.com 等网站建立 HttpURLConnection 并解析它:-)

2021-09-15T00:23:37   回复
IT小君

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

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();
        }
    }
}
2021-09-15T00:23:37   回复
IT小君

http://jstun.javawi.de/会做 - 只要你的网关设备做 STUN )大多数做)

2021-09-15T00:23:38   回复
IT小君

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

我建议你通过打开一个像http://www.whatismyip.com/这样的网站并通过解析 html 结果来获取 IP 号来利用它......它不应该那么难!

2021-09-15T00:23:38   回复
IT小君

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

2021-09-15T00:23:38   回复
IT小君
System.out.println(pageCrawling.getHtmlFromURL("http://ipecho.net/plain"));
2021-09-15T00:23:38   回复
IT小君

另一种解决方案是执行外部命令,显然,这种解决方案限制了应用程序的可移植性。

例如,对于运行在 Windows 上的应用程序,可以通过 jPowershell 执行 PowerShell 命令,如下代码所示:

public String getMyPublicIp() {
    // PowerShell command
    String command = "(Invoke-WebRequest ifconfig.me/ip).Content.Trim()";
    String powerShellOut = PowerShell.executeSingleCommand(command).getCommandOutput();

    // Connection failed
    if (powerShellOut.contains("InvalidOperation")) {
        powerShellOut = null;
    }
    return powerShellOut;
}
2021-09-15T00:23:39   回复