ip

Resolve IP to Country Name in Java

Getting country name from IP address can be done offline with help of a simple Java library.

Download IP2C file from Here- http://files.firestats.cc/ip2c/ip2c-2.0.0.zip

 

IP2C resolves country names from ip using a binary File. It uses database provided by Webhosting.info and also supports Software77  Database.

It converts the CSV database into binary file and supports the search straight on the file ,in memory or in a memory mapped file.

Here are Steps to use this library with Java.

  • Extract the downloaded file
  • Add ip2c.jar to the Build Path 
  • Copy ip-to-country.bin file into your project folder
Usage (In Memory):
import java.io.IOException;
import net.firefang.ip2c.Country;
import net.firefang.ip2c.IP2Country;

public class ip {

	public static void main(String args[]) throws IOException{

	String iptest = "180.149.247.26";
	int caching1 = IP2Country.MEMORY_CACHE;
	IP2Country ip2c = new IP2Country(caching1);
	Country c = ip2c.getCountry(iptest);
	if (c == null)	{
	        System.out.println("UNKNOWN");                          
	}
	else{
	        System.out.println(c.getName());      
	}
	}
}

 

For Usage with PHP and other details see IP2C

One thought on “Resolve IP to Country Name in Java”

  1. Give proper details where to place bin file.When placing it in project folder the error is file not found….

Leave a Reply

Your email address will not be published. Required fields are marked *