GeoIP定位查询 Java篇
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
相关推荐
鲁氏汤包王 2020-01-09
zrtlin 2019-08-31
liuchen0 2019-08-30
hwhanwan 2011-06-30
Wenjing 2017-07-18
superyu 2011-04-13
liyingjie0 2011-08-10
cullinans 2019-06-26
hagetis 2019-06-25
liuyaping 2009-04-27
subnetwork 2015-05-14
87497001 2014-03-28
linuxvfast 2014-01-19
Xinspace 2012-11-10
HatsuneMiku 2017-09-28
思考的稻草人 2017-08-12
andyzhaojianhui 2016-01-14
Movenow 2014-06-17
聚指尖 2013-06-14