Getting the 'external' IP address in Java

前端 未结 12 1695
自闭症患者
自闭症患者 2020-11-22 15:08

I\'m not too sure how to go about getting the external IP address of the machine as a computer outside of a network would see it.

My following IPAddress class only g

相关标签:
12条回答
  • 2020-11-22 15:52

    One of the comments by @stivlo deserves to be an answer:

    You can use the Amazon service 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();
                    }
                }
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-22 15:55

    As @Donal Fellows wrote, you have to query the network interface instead of the machine. This code from the javadocs worked for me:

    The following example program lists all the network interfaces and their addresses on a machine:

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

    The following is sample output from the example program:

    Display name: TCP Loopback interface
    Name: lo
    InetAddress: /127.0.0.1
    
    Display name: Wireless Network Connection
    Name: eth0
    InetAddress: /192.0.2.0
    

    From docs.oracle.com

    0 讨论(0)
  • 2020-11-22 15:58

    An alternative solution is to execute an external command, obviously, this solution limits the portability of the application.

    For example, for an application that runs on Windows, a PowerShell command can be executed through jPowershell, as shown in the following code:

    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;
    }
    
    0 讨论(0)
  • 2020-11-22 16:01

    It's not that easy since a machine inside a LAN usually doesn't care about the external IP of its router to the internet.. it simply doesn't need it!

    I would suggest you to exploit this by opening a site like http://www.whatismyip.com/ and getting the IP number by parsing the html results.. it shouldn't be that hard!

    0 讨论(0)
  • 2020-11-22 16:05

    http://jstun.javawi.de/ will do it - provided your gateway device does STUN )most do)

    0 讨论(0)
  • 2020-11-22 16:05
    System.out.println(pageCrawling.getHtmlFromURL("http://ipecho.net/plain"));
    
    0 讨论(0)
提交回复
热议问题