A simple to use Java library for the freely available DB-IP IP address to city dataset. Requires Java 8
#Before you begin The entire dataset is loaded into a TreeMap . Make sure that you have about 2 GB of Heap space available to load it.
#Get
With maven :
<dependency>
<groupId>in.ankushs</groupId>
<artifactId>Java-DB-IP</artifactId>
<version>2.0</version>
</dependency>
Or gradle:
compile('in.ankushs:Java-DB-IP:2.0')
The Javadocs for the latest release can be found here
#Instructions
In order to get geographical information for an ip address, just pass the dbip-city-latest.csv.gz
as a File object to DbIpClient
as follows:
File gzip = new File(PATH_TO_dbip-city-latest.csv.gz);
DbIpClient client = new DbIpClient(gzip);
Once the data is loaded from the file into memory , any subsequent invocation of the above code would not re-load the data .
Next,just fetch the data for a ip ,like so :
DbIpClient client = new DbIpClient(gzip);
GeoEntity geoEntity = client.lookup("31.45.127.255");
String city = geoEntity.getCity();
String country = geoEntity.getCountry();
String province = geoEntity.getProvince();
String countryCode = geoEntity.getCountryCode();
System.out.println("city : " + city);
System.out.println("province : " + province);
System.out.println("country : " + country);
System.out.println("country code : " + countryCode);
This prints :
city : Oslo
province : Oslo
country : Norway
country code : NO
For IPV6, do the same :
DbIpClient client = new DbIpClient(gzip);
String ipv6 = "2c0f:f468:ffff:ffff:ffff:ffff:ffff:ffff";
GeoEntity geoEntity = client.lookup(ipv6);
That's pretty much it.
#Integrating with Spring Boot. Follow the detailed instructions on this blogpost . You can find the Spring Boot Java-DB-IP project here .