首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

IP 地址批量查询!多谢,解决就结贴

2012-03-19 
IP 地址批量查询!!在线等,谢谢,解决就结贴!手头有5314个IP,大概有美国,台湾,香港的,我想知道他们都是哪里

IP 地址批量查询!!在线等,谢谢,解决就结贴!
手头有5314个IP,大概有美国,台湾,香港的,我想知道他们都是哪里的!http://www.ip138.com/只能一个个的查询,太慢了!

[解决办法]
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.PostMethod;

public class IP {

  public static void main(String[] args) throws HttpException, IOException {  
    Map <String, String> map = new LinkedHashMap <String, String> ();
    map.put( "211.100.26.124 ", " ");
    map.put( "133.13.233.11 ", " ");
    map.put( "209.131.36.158 ", " ");
    map.put( "138.175.248.10 ", " ");
    map.put( "60.12.227.64 ", " ");
    map.put( "72.21.206.80 ", " ");
    map.put( "61.152.173.36 ", " ");
    long t0 = System.currentTimeMillis();
    int i = 0;
    int size = map.size();
    for (Map.Entry <String, String> entry : map.entrySet()) {
      System.out.printf( "[%d/%d] 正在解析 %s ", ++i, size, entry.getKey());
      String url = "http://www.ip138.com/ips8.asp ";
      HttpClient client = new HttpClient();
      PostMethod method = new PostMethod(url);
      method.setParameter( "action ", "2 ");
      method.setParameter( "ip ", entry.getKey());
      client.executeMethod(method);

      String response = method.getResponseBodyAsString();
      response = new String(response.getBytes( "iso-8859-1 "), "gb2312 ");
      response = response.substring(response.indexOf( "本站主数据: "), response.indexOf( " </li> </ul> "));
      map.put(entry.getKey(), response);
      System.out.println( ",解析完成 ");
      method.releaseConnection();
    }
    System.out.printf( "解析 5314 个 IP 可能要花:%.2f 小时%n ", (System.currentTimeMillis() - t0) * 5314 / (1000.0 * map.size()) / 3600);
    
    // 输出
    //for (Map.Entry <String, String> entry : map.entrySet()) {
    //  System.out.println(entry.getKey() + " --> " + entry.getValue());
    //}
    System.out.println( "OK ");
  }
}

以上代码,需要使用 Commons HttpClient 类库
http://commons.apache.org/downloads/download_httpclient.cgi

以及依赖类库:Commons Logging 和 Commons Codec 类库
http://commons.apache.org/downloads/download_logging.cgi
http://commons.apache.org/downloads/download_codec.cgi

测试了一下解析 5314 个 IP 大约要花两个小时左右的时间。

热点排行