根据GeoIP快速把IP转换成经纬度(Java版)

GeoIP jar包:geoip-api-1.3.1.jar
下载地址:http://pan.baidu.com/s/1kU5X0RP
Geo city dat文件:GeoLiteCity-2013-01-18.dat
下载地址:
老数据地址:http://pan.baidu.com/s/1eRVkkgy
官方更新地址:(找到城市的下载即可)http://dev.maxmind.com/geoip/legacy/geolite/

把dat文件放在自己的本地目录,然后项目中导入geoip.jar即可:

import com.maxmind.geoip.Location;
import com.maxmind.geoip.LookupService;
import java.io.IOException;

public class TestMain {
public static void main(String[] args) {
try {
LookupService cl = new LookupService("D:/lib/geoip/GeoLiteCity-2013-01-18.dat", LookupService.GEOIP_MEMORY_CACHE);
Location l2 = cl.getLocation("144.0.9.29");
System.out.println(
"countryCode: " + l2.countryCode +"\n"+
"countryName: " + l2.countryName +"\n"+
"region: " + l2.region +"\n"+
"city: " + l2.city +"\n"+
"latitude: " + l2.latitude +"\n"+
"longitude: " + l2.longitude);
} catch (IOException e) {
e.printStackTrace();
}
}
}
输出内容:

countryCode: CN
countryName: China
region: 25
city: Jinan
latitude: 36.668304
longitude: 116.99719

标签: none

添加新评论