Package com.lovo.util;
Import Java.io.BufferedReader;
Import Java.io.InputStream;
Import Java.io.InputStreamReader;
Import java.net.HttpURLConnection;
Import Java.net.URL; public class Publicinterip {/** * @param args * @throws Exception/public String Getpublicip () {try {Str ing path = "http://iframe.ip138.com/ic.asp";//To get the address URL URL of HTML page content = new URL (path);//Create URL object httpurlconnection conn = (httpurlconnection) url.openconnection ();//Open Connection Conn.setrequestproperty ("ContentType", "GBK"); Set URL Chinese parameter encoding conn.setconnecttimeout (5 * 1000);//Requested Time Conn.setrequestmethod ("get");//Request Way InputStream Inst
Ream = Conn.getinputstream ();
Readlesosysxml (instream);
BufferedReader in = new BufferedReader (new InputStreamReader (instream, "GBK"));
StringBuffer buffer = new StringBuffer ();
String line = "";
Reads the last line fetched to the content, writes while (line = In.readline ())!= null) {buffer.append (line);
String str = buffer.tostring ();String ipString1 = str.substring (Str.indexof ("[")); Get your IP is in the middle of [182.149.82.50] content String ipsString2 = ipstring1.substring (Ipstring1.indexof ("[") + 1, Ipstring1.lasti
Ndexof ("]")); Gets the address of the current IP address/* String ipsstring3=ipstring1.substring (Ipstring1.indexof (":"), Ipstring1.lastindexof ("</center
> "));
System.err.println (IPSSTRING3);//Back to public IP value return ipsString2;
catch (Exception e) {System.out.println ("Get public network IP Connection timeout");
Return "Connection Timeout";
} public static void Main (string[] args) {Publicinterip interip=new publicinterip ();
System.err.println (Interip.getpublicip ()); }
}