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

KhA*_*aAb 5 java ip location

我正在寻找示例代码来获取 IP 的确切位置。我仔细看了看,但没有找到任何示例代码。谢谢..

nul*_*ter 3

我之前使用 MaxMind GeoIP Lite 数据库取得了很好的成功。城市和国家数据库API均可用。

用法示例如下:

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);
}
Run Code Online (Sandbox Code Playgroud)