This question already has an answer here:
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..
I've used the MaxMind GeoIP Lite database to good success before. Both city and country databases and APIs are available.
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);
}
来源:https://stackoverflow.com/questions/16185703/sample-code-for-finding-location-using-ip-address-in-java