public static void main(String[] args) {
String inputLine = "";
String read = "";
String ip = "";
try {
URL url = new URL("http://checkip.dyndns.org/");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
while ((read = in.readLine()) != null) {
inputLine += read;
}
System.out.println(inputLine);
String[] strs = inputLine.split(":");
ip = strs[1].split("<")[0];
System.out.println(ip);
} catch (Exception e) {
e.printStackTrace();
}
}
http://checkip.dyndns.org/ 不够稳定
可以换http://www.myip.cn/
String[] strs = inputLine.split("您的IP地址:");