1 package ipconfig; 2 3 import java.io.BufferedReader; 4 import java.io.IOException; 5 import java.io.InputStreamReader; 6 import java.net.MalformedURLException; 7 import java.net.URL; 8 import java.net.URLConnection; 9 10 public class gainInformation { 11 public static void main(String args[]) { 12 String publicip = getip.publicip(); 13 int ipindex1 = publicip.indexOf("["); 14 int ipindex2 = publicip.indexOf("]"); 15 String ip = publicip.substring(ipindex1+1, ipindex2); 16 System.out.println(ip); 17 int address1 = publicip.indexOf("来自:"); 18 int address2 = publicip.indexOf("</center>"); 19 String substring = publicip.substring(address1+3,address2); 20 String[] split = substring.split(" "); 21 for (int i = 0; i < split.length; i++) { 22 System.out.println(split[i]); 23 } 24 } 25 } 26 class getip{ 27 public static String publicip() { 28 BufferedReader br = null; 29 try { 30 URL url = new URL("http://20019.ip138.com/ic.asp"); 31 URLConnection urlconn = url.openConnection(); 32 br = new BufferedReader(new InputStreamReader(urlconn.getInputStream())); 33 String buf,get = null; 34 while ((buf = br.readLine()) != null) { 35 get+=buf; 36 } 37 return get; 38 } catch (MalformedURLException e) { 39 e.printStackTrace(); 40 } catch (IOException e) { 41 e.printStackTrace(); 42 } finally { 43 try { 44 br.close(); 45 } catch (IOException e) { 46 e.printStackTrace(); 47 } 48 } 49 return null; 50 } 51 }