Class LookupService

java.lang.Object
com.maxmind.geoip.LookupService

public class LookupService
extends Object
Provides a lookup service for information based on an IP address. The location of a database file is supplied when creating a lookup service instance. The edition of the database determines what information is available about an IP address. See the DatabaseInfo class for further details.

The following code snippet demonstrates looking up the country that an IP address is from:

 // First, create a LookupService instance with the location of the database.
 LookupService lookupService = new LookupService("c:\\geoip.dat");
 // Assume we have a String ipAddress (in dot-decimal form).
 Country country = lookupService.getCountry(ipAddress);
 System.out.println("The country is: " + country.getName());
 System.out.println("The country code is: " + country.getCode());
 
In general, a single LookupService instance should be created and then reused repeatedly.

Tip: Those deploying the GeoIP API as part of a web application may find it difficult to pass in a File to create the lookup service, as the location of the database may vary per deployment or may even be part of the web-application. In this case, the database should be added to the classpath of the web-app. For example, by putting it into the WEB-INF/classes directory of the web application. The following code snippet demonstrates how to create a LookupService using a database that can be found on the classpath:

 String fileName = getClass().getResource("/GeoIP.dat").toExternalForm()
         .substring(6);
 LookupService lookupService = new LookupService(fileName);
 
Author:
Matt Tucker (matt@jivesoftware.com)