`
baiiiuuu
  • 浏览: 43505 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

GeoIP定位查询 Java篇

    博客分类:
  • 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
分享到:
评论
1 楼 技术宅男 2012-03-06  
我网上下的是GeoLiteCity.dat跟你的GeoIP.dat是一样的不?

相关推荐

Global site tag (gtag.js) - Google Analytics