用Java获取"外部"IP地址

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)

  • 我建议至少实现两个为你的ip服务的服务器,以防其中一个服务器现在停机(当然正确的错误处理).我的选择是http://www.externalip.net(点击"需要一个API?"链接).现在已经使用该网站超过一年,到目前为止没有任何问题,这是一个快速响应的快速服务. (8认同)
  • Whatsmyip.com似乎不再支持这种类型的自动化了. (7认同)
  • 警告:他们已将自动化网址调整为http://automation.whatismyip.com/n09230945.asp (4认同)

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地址.这里的其他答案提供了涉及与外部网站交谈的方法的技巧.

  • 或者不是,因为它没有回答原始问题,它看起来像是一个学术练习 (3认同)

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)

来自docs.oracle.com


Fra*_*uza 11

所有这些仍然运行顺利!(截至2016年12月19日)

建议:不要直接依赖其中一个; 尝试使用一个,但考虑到其他人有一个contigency计划!你使用的越多越好!

祝好运!


nc3*_*c3b 5

对像www.whatismyip.com这样的网站进行HttpURLConnection并解析:-)


小智 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)