在java中使用IP地址查找位置的示例代码?

时间:2013-04-24 07:15:50

标签: java location ip

我正在寻找示例代码来获取IP的精确位置。 我做了goggling但没有找到任何示例代码。 谢谢..

1 个答案:

答案 0 :(得分:2)

我之前使用MaxMind GeoIP Lite数据库取得了很好的成功。 city and country databasesAPIs都可用。

使用的一个例子是:

File dbfile = new File("db/GeoLiteCity.dat");
LookupService lookupService = new LookupService(dbfile, LookupService.GEOIP_MEMORY_CACHE);

Location location = lookupService.getLocation(ipAddress);

// Populate region. Note that regionName is a MaxMind class, not an instance variable
if (location != null) {
    location.region = regionName.regionNameByCode(location.countryCode, location.region);
}