chenchuanwen 2011-11-28
GeoIP数据库下载地址:http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
API源码下载
http://geolite.maxmind.com/download/geoip/api/java/
下载解压:有一些测试类“*test.java”,如CountryLookupTest.java
将所有源码拷贝在一个java工程下面...
改下源码的Geoip.dat文件的目录:我是放在C盘;
代码:
class CountryLookupTest { public static void main(String[] args) { try { // String sep = System.getProperty("file.separator"); // // // Uncomment for windows // String dir = System.getProperty("user.dir"); // // // Uncomment for Linux // //String dir = "/usr/local/share/GeoIP"; // // String dbfile = dir + sep + "GeoIP.dat"; // You should only call LookupService once, especially if you use // GEOIP_MEMORY_CACHE mode, since the LookupService constructor // takes up // resources to load the GeoIP.dat file into memory // LookupService cl = new // LookupService(dbfile,LookupService.GEOIP_STANDARD); LookupService cl = new LookupService("c:\\GeoIP.dat", LookupService.GEOIP_MEMORY_CACHE); System.out.println(cl.getCountry("159.226.115.22").getCode()); System.out.println(cl.getCountry("159.226.115.22").getName()); System.out.println(cl.getCountry("183.16.200.127").getName()); System.out.println(cl.getCountry("213.52.50.8").getName()); System.out.println(cl.getCountry("200.21.225.82").getName()); cl.close(); } catch (IOException e) { System.out.println("IO Exception"); } } }
运行结果:
CN
China
China
Norway
Colombia