Getting the 'external' IP address in Java

前端 未结 12 1711
自闭症患者
自闭症患者 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:46

    How about this? It's simple and worked the best for me :)

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

    I am not sure if you can grab that IP from code that runs on the local machine.

    You can however build code that runs on a website, say in JSP, and then use something that returns the IP of where the request came from:

    request.getRemoteAddr()

    Or simply use already-existing services that do this, then parse the answer from the service to find out the IP.

    Use a webservice like AWS and others

    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);
    
    0 讨论(0)
  • 2020-11-22 15:47

    If you are using JAVA based webapp and if you want to grab the client's (One who makes the request via a browser) external ip try deploying the app in a public domain and use request.getRemoteAddr() to read the external IP address.

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

    The truth is: 'you can't' in the sense that you posed the question. NAT happens outside of the protocol. There is no way for your machine's kernel to know how your NAT box is mapping from external to internal IP addresses. Other answers here offer tricks involving methods of talking to outside web sites.

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

    All this are still up and working smoothly! (as of 23 Sep 2019)

    • http://checkip.amazonaws.com/
    • https://ipv4.icanhazip.com/ (works with ipv6 as subdomain too!)
    • http://myexternalip.com/raw
    • http://ipecho.net/plain
    • http://bot.whatismyipaddress.com
    • http://www.trackip.net/ip (only gives ipv6)
    • http://curlmyip.com/ (17 Dec 2016)

    Piece of advice: Do not direcly depend only on one of them; try to use one but have a contigency plan considering others! The more you use, the better!

    Good luck!

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

    Make a HttpURLConnection to some site like www.whatismyip.com and parse that :-)

    0 讨论(0)
提交回复
热议问题