在Java中想要使用这个数据文件需要下载相应的Jar包和dat文件:
GeoIP jar包:geoip-api-1.3.1.jar
Geo city dat文件:GeoLiteCity.dat
把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();
}
}
}
1