- import java.util.regex.*;
- import java.net.*;
- import java.util.*;
- import java.io.*;
- //获取外网IP和IP所属地
- public class Test {
- public static void main(String[] args){
- BufferedReader br = null;
- ArrayList<String> s = new ArrayList<String>();
- try{
- URL u = new URL("http://www.ip.cn/");
- URLConnection url = u.openConnection();
- br = new BufferedReader(new InputStreamReader(url.getInputStream(),"UTF-8"));
- String str = null;
- String reg = "当前 IP:<code>[0-9\\.]+</code>";
- String str2 = null;
- String str3 = null;
- String reg3 = "来自:.+</p><p>";
- while((str = br.readLine())!=null){
- Pattern p = Pattern.compile(reg);
- Matcher m = p.matcher(str);
- while(m.find()){
- str2 = m.group();
- }
- p = Pattern.compile(reg3);
- m = p.matcher(str);
- while(m.find()){
- str3 = m.group();
- }
- }
- String reg2 = "[0-9].+[0-9]";
- Pattern p = Pattern.compile(reg2);
- Matcher m = p.matcher(str2);
- while(m.find()){
- System.out.print("外网IP:"+m.group());
- }
- reg3 = "[^</>p来自:]";
- p = Pattern.compile(reg3);
- m = p.matcher(str3);
- while(m.find()){
- s.add(m.group());
- }
- System.out.print("\n地址:");
- for(String ss : s){
- System.out.print(ss);
- }
- }
- catch(IOException e){
- throw new RuntimeException("读入文件出错");
- }
- }
- }
复制代码 |
|