How to use Java to map IP addresses to countries and regions

Source: Internet
Author: User
IP address to country Mapping

This is a how-to on IP address to country mapping in Java.

The first step

The starting point in IP address to country mapping is to obtain some tables with the IP
Address to country mapping data in them. IP address to country Mapping
Tables are to be found at the webnet77 website. The table contains up to about 65000 rows.
Download it, decompress it and look at it in a text editor. now remove all the lines starting with a hash sign, which is this sign #. you first line shoshould now look something like this:

   "50331648","67108863","ARIN","572572800","US","USA","UNITED STATES"
The second step

In my case I was interested in the from field, the to field
And the country code field. You may want the country Abbreviation and/or the country name fields in addition to the from and to fields. Create
A database table with the fields you want. I made the numerical fields bigints.

I use MySQL. therefore my interest was to convert the file to a tab separated list of rows. perl will be very good for this, but I don't know Perl. therefore I wrote a small Java class that is used from the command
Line to do this. Here is the code:

/** * Changes the IpToCountry.csv file as downloaded  * (after taking away the portions following the # and any empty lines)  * from http://software77.net/cgi-bin/ip-country/geo-ip.pl * to a tab separated file usable by MySQL */import java.io.*;import java.util.StringTokenizer;public class FileChanger{   private StringBuffer buffer;   public FileChanger(String inputFile, String outputFile){    buffer = new StringBuffer();go(inputFile, outputFile);   }      private void go(String inputFile, String outputFile){try{   FileReader fileReader = new FileReader(inputFile);    BufferedReader reader = new BufferedReader(fileReader);   if(reader.ready()){String read = reader.readLine();while(read != null){   StringTokenizer tokens = new StringTokenizer(read, ",", false);   String from = tokens.nextToken();   String to = tokens.nextToken();   // A few entries we are not interested in before we get to    // the real country code   String countryCode = tokens.nextToken();   countryCode = tokens.nextToken();   countryCode = tokens.nextToken();   // Replace any occurences of UK with GB   if(countryCode.equalsIgnoreCase("\"UK\""))countryCode = "GB";   String line = from + "\t" + to + "\t" + countryCode + "\n";   // Remove all quotes   line = line.replaceAll("\"", "");   buffer = buffer.append(line);   read = reader.readLine();}fileReader.close();reader.close();PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(outputFile)));out.write(buffer.toString());out.flush();out.close();   }}catch(Exception e){   System.out.println("Exception: " + e.getMessage());}   }   public static void main(String[] args){if(args.length != 2){   System.out.println("Usage: java FileChanger inputFile outputFile");   System.exit(1);}FileChanger fileChanger = new FileChanger(args[0], args[1]);   }}

You will notice that all occurences of UK are replaced with GB. That is not necessary if you do not want to do it.

The lines of the file created by filechanger look like this:

5033164867108863US6710886483886079US100663296117440511US117440512134217727US

You can of course adapt filechanger to do what you want it to do.

Step three

Now zip or gzip this file and upload it to the server where your web-application is hosted. obviusly, if it is your local machine or if you have broadband or cable, compressing is not necessary. on the other
Side decompress it and do the following on the command line from the directory where the decompressed plain text file lives. You may have given it any extension or none at all, no problem.

mysql -u yourUserName yourDatabaseName -p

You will be prompted for your password and connected to your database. Now do the following:

load data local infile "ipData.txt" into table ip2country_tbl;

About half a second after hitting Enter all nearly 65000 rows will be loaded, DV.

Step four

Now to get hold of the IP address the person visiting your website is ing from. the following code comes from. JSP-a Java Server Page. it checks for proxies on the host server. here it is:

String ipAddress = null;if (request.getHeader("X-Forwarded-For") == null)    ipAddress = request.getRemoteAddr();else    ipAddress = request.getHeader("X-Forwarded-For");String countryCode = codeBean.getCountryCode(ipAddress);
The host I am with uses a proxy and I get an IP Address returned in the following format:
169.23.123.89,127.0.0.46

Obviously, I am only interested in the 169.23.123.89 part. The code that will soon follow tests
for a comma and then chops off everything from the comma onwards, including the comma. codeBean really only passes the String IP Address to a
Stateless Session Bean, gets the returned value from the Stateless Session Bean and passes that to the JSP. Here is how the Stateless Session Bean converts the String IP Address to a java.lang.Long as stored in the database and queries the database using this
Long. I'm including a the XDoclet tags in case anybody is interested.

/** *  * @param IPAddress * @return a String the country code *  * @ejb.interface-method * view-type="remote" *  **/public String getCountryCode(String IPAddress){logman.debug("The IP Address passed in is " + IPAddress);// Chop off everything from the comma onwardsStringBuffer buffer = new StringBuffer(IPAddress);int index = buffer.indexOf(",");// See if there is  commaif(index > 0){   int length = buffer.length();   buffer = buffer.delete(index, length);}StringTokenizer tokens = new StringTokenizer(buffer.toString(), ".", false);long answer = 0L;int counter = 3;while(tokens.hasMoreTokens() && counter >= 0){long read = new Long(tokens.nextToken()).longValue();long calculated = new Double(read *(Math.pow(256, counter))).longValue();answer += calculated;counter --;logman.debug("Iteratrions read backward - 3,2,1 no:  " + (counter +1));}Long IPValue = new Long(answer);logman.debug("The IP Address value is: " + IPValue.toString());try {IP2CountryCMPLocalHome ip2countryLocalHome = IP2CountryCMPUtil.getLocalHome();return ip2countryLocalHome.getCountryCode(IPValue);}catch (NamingException e) {logman.error(e.getMessage());return "GB";}}
You will notice some Log4J debugging code. I leave it and just change the log level. Very handy. The bit in the try clause is where the database is queried and the result returned. In actual fact the IP2CountryCMPBean (container managed persistence bean) is cached in memory and the database won't be accessed until the values in this table change again. You will know how this speeds things up.

IP to Country Mapping seems to be ideal for a Web Service. As you can see, it will not be difficult to do either.

Well, that's all folks.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.