java 在java中使用IP地址查找位置的示例代码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16185703/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sample code for finding location using IP address in java?
提问by KhAn SaAb
I am looking for the sample code to get Exact Location of IP. I did goggling but haven't find any Sample code. Thank You..
我正在寻找示例代码以获取 IP 的确切位置。我确实目击了但还没有找到任何示例代码。谢谢..
回答by nullPainter
I've used the MaxMind GeoIP Lite database to good success before. Both city and country databasesand APIsare available.
我以前使用 MaxMind GeoIP Lite 数据库取得了成功。这两个城市和国家数据库和API的可供选择。
An example of usage is:
使用示例是:
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);
}