目的:获取ip对应的地理位置
方法:从http://ftp.apnic.net/apnic/stats/apnic/delegated-apnic-latest获取国际最新的ip分布信息,然后筛选出中国的ip使用http://whois.pconline.com.cn提供的接口获取ip的具体位置,解析结果,保存。
代码:
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.json.JSONException;
import org.json.JSONObject;
public class IpStore {
private IpStoreService ipStoreService;
public void main() {
List<Map<String, Object>> ipStore = null;
// 获取ip段
try {
ipStore = this.getIpRange();
System.out.println("获取ip段成功" + ipStore.size());
} catch (Exception e) {
e.printStackTrace();
}
// 获取ip数据(json格式)
int count=1;
for (Map<String, Object> ip : ipStore) {
try {
if(count++%1000==0)
System.out.println(new Date()+"正在获取ip,请稍后……"+count);
String jsonIp = this.getIpInfoByPconline(ip.get("ip")
.toString());
JSONObject json = new JSONObject(jsonIp);
// 对省份进行处理
ip.put("ip_province", this.trimProvince(json.get("pro")
.toString()));
ip.put("ip_city", this.trimCity(json.get("city").toString()));
ip.put("ip_address", json.get("addr"));
// 处理ip范围ip_s1.ip_s2.ip_s3.ip_s4-ip_e1.ip_e2.ip_e3.ip_e4
int[] ip_detail = ipStringToInt(ip.get("ip").toString(), ip
.get("ipRange").toString());
for (int i = 0; i < 4; i++) {
ip.put("ip_s" + (i % 4 + 1), ip_detail[i]);
ip.put("ip_e" + (i % 4 + 1), ip_detail[i + 4]);
}
} catch (Exception e) {
e.printStackTrace();
}
}
/* for (Map<String, Object> ip : ipStore) {
System.out.print(ip.get("ip_s1") + "." + ip.get("ip_s2") + "."
+ ip.get("ip_s3") + "." + ip.get("ip_s4"));
System.out.print("--");
System.out.print(ip.get("ip_e1") + "." + ip.get("ip_e2") + "."
+ ip.get("ip_e3") + "." + ip.get("ip_e4"));
System.out.print("\t" + ip.get("ip_province") + "\t"
+ ip.get("ip_city") + "\t" + ip.get("ip_address"));
System.out.println();
}
*/
//将集合入库
}
// 获取ip范围 ip为起始ip ipRange为数量
public List<Map<String, Object>> getIpRange()
throws UnsupportedEncodingException, IOException {
URL url = new URL(
"http://ftp.apnic.net/apnic/stats/apnic/delegated-apnic-latest");
URLConnection con = url.openConnection();
BufferedReader reader = new BufferedReader(new InputStreamReader(con
.getInputStream(), "GBK"));
String info = null;
// int i=100;
List<Map<String, Object>> ipRange = new ArrayList<Map<String, Object>>();
while ((info = reader.readLine()) != null) {
if (info.startsWith("apnic|")) {
String[] str = info.split("[|]");
if (str[2].equals("ipv4")&&str[3].matches("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}")) {
Map<String, Object> ipInfo = new HashMap<String, Object>();
ipInfo.put("ip", str[3]);
ipInfo.put("ipRange", str[4]);
ipRange.add(ipInfo);
// if(i--<0)
// break;
}
}
}
reader.close();
return ipRange;
}
// 通过ip从网上库中获取ip数据 json格式
public String getIpInfoByPconline(String ip) throws IOException {
URL url = new URL("http://whois.pconline.com.cn/ipJson.jsp?ip=" + ip);
URLConnection con = url.openConnection();
BufferedReader reader = new BufferedReader(new InputStreamReader(con
.getInputStream(), "GBK"));
String info = null;
info = reader.readLine();
reader.close();
return info.substring(34, info.length() - 3);
}
public void saveIpInfo() {
}
// 处理ip范围ip_s1.ip_s2.ip_s3.ip_s4-ip_e1.ip_e2.ip_e3.ip_e4依次对应返回值得数组中的值
public int[] ipStringToInt(String ip, String ipRange) {
int[] ipInt = new int[8];
String[] ips = ip.split("\\.");
if(ips.length!=4){
return null;
}
for (int i = 0; i < 4; i++) {
ipInt[i] = Integer.parseInt(ips[i]);
ipInt[i + 4] = ipInt[i];
}
int range = Integer.parseInt(ipRange);
long ipLong = ipInt[0] * (1l << 24) + ipInt[1] * (1l << 16) + ipInt[2]
* (1l << 8) + ipInt[3];
ipLong += range;
ipInt[7] = (int) (ipLong % 256);
ipLong >>= 8;
ipInt[6] = (int) (ipLong % 256);
ipLong >>= 8;
ipInt[5] = (int) (ipLong % 256);
ipLong >>= 8;
ipInt[4] = (int) (ipLong % 256);
return ipInt;
}
// 从文件中获取ip范围
public List<Map<String, Object>> getIpRange2() throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(
new FileInputStream("d:/delegated-apnic-latest.txt")));
String info = null;
List<Map<String, Object>> ipRange = new ArrayList<Map<String, Object>>();
int i = 10;
while ((info = reader.readLine()) != null) {
if (info.startsWith("apnic|")) {
String[] str = info.split("[|]");
if (str[2].equals("ipv4")&&str[3].matches("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}")) {
Map<String, Object> ipInfo = new HashMap<String, Object>();
ipInfo.put("ip", str[3]);
ipInfo.put("ipRange", str[4]);
ipRange.add(ipInfo);
if (--i < 0)
break;
}
}
}
reader.close();
return ipRange;
}
// 解决获取数据省份名称不对应问题
public String trimProvince(String pro) {
if (pro.endsWith("省") || pro.endsWith("市"))
pro = pro.substring(0, pro.length() - 1);
return pro;
}
// 解决获取数据城市名称不对应问题
public String trimCity(String city) {
return city;
}