Geolocation IP in Java [closed]

2020-05-08 08:40发布

问题:

Is there any way to get the country, city (generally: geolocation information) from IP address. Note that my application is java.

I visisted and tried many web services, but I didn't find suitable one and I prefer to write my own. Also, I found some CSV DBs but also this solution is not preferable at all. please, help.

回答1:

All you need is GeoIP, provided by MaxMind company.

They provide free GeoIP2 database - GeoLite2 and several GeoIP2 APIs(thanks karel) including Java API to read database file too.

  • Documents
  • Java API
  • Downloadable Java libraries
  • Downloadable Java libraries from maven.org, my favorite :), you need download both com.maxmind.db and com.maxmind.geoip2.

Before GeoIP2, there was GeoIP, now they called it GeoIP Lecacy, Free databases and downloadable Java library from maven.org or from github are still available.

GeoIP Lecacy maybe dead in the future, however, for now, because GeoIP2 doesn't provide Java API to read the database information (such as GEO-533LITE 20150303 Build 1 Copyright (c) 2015 MaxMind Inc All Rights Reserved), so I still use it in my website. I use GeoIP2 in my IRC bot to benefit from the multiple language feature of GeoIP2.



回答2:

Each country has a few range of IPs.

I'm not aware of a Java implementation of that, but you could use the output from http://services.ce3c.be/ciprg/ and create your own code that gives the country based upon the IP address.