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

java中使用IP地址查找位置的示例代码?,java,location,ip,Java,Location,Ip,我正在寻找样本代码,以获得IP的确切位置。 我看了一下,但没有找到任何示例代码。 谢谢。我以前使用MaxMind GeoIP Lite数据库取得了很大成功。和都有 用法的一个例子是: File dbfile = new File("db/GeoLiteCity.dat"); LookupService lookupService = new LookupService(dbfile, LookupService.GEOIP_MEMORY_CACHE); Location location =

我正在寻找样本代码,以获得IP的确切位置。 我看了一下,但没有找到任何示例代码。
谢谢。

我以前使用MaxMind GeoIP Lite数据库取得了很大成功。和都有

用法的一个例子是:

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);
}

让我测试一下。nullPainterNote MaxMind数据库的免费版本更新的频率低于商业版本。还要注意的是,“精确位置”的概念在IP方面是相对的,可能基于ISP所在的城市,取决于您所在国家的大小、ISP等。检查ip2asn2cc库:在这里试试这个代码这个答案解决过您的问题吗,@KhAn SaAb?如果是这样的话,如果它能被标记为已接受就太好了。