Home >Java >javaTutorial >How Can I Get a Machine\'s External IP Address in Java?
Getting the Machine's External IP Address in Java
Locating a machine's external IP address, as viewed by devices outside its network, can be a challenge in Java. The provided IpAddress class only fetches the local IP address. To obtain the external IP, consider the following solutions:
External Services
It may not be feasible to retrieve the external IP from code running on the local machine. Instead, you can:
Web Services
One effective method involves employing web services like 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);
The above is the detailed content of How Can I Get a Machine\'s External IP Address in Java?. For more information, please follow other related articles on the PHP Chinese website!