Query IP Address Location in Bulk using Java
Submitted by hexahow on Saturday, November 19, 2016 - 20:07.
This Java code lookup the IP2Location info from an IP address.
Things that we can do with this script.
1. Redirect based on country
2. Digital rights management
3. Web log stats and analysis
4. Auto-selection of country on forms
5. Filter access from countries you do not do business with
6. Geo-targeting for increased sales and click-through
The sample can be requested at https://www.ip2location.com/software/java-component
- import com.ip2location.*;
- import java.io.File;
- import java.io.IOException;
- import java.util.Scanner;
- import java.util.ArrayList;
- import java.util.List;
- public class Main
- {
- public Main()
- {
- }
- {
- /*
- * Steps:
- * 1.Copy the Main.java from the "demos" folder
- * 2.Configure Build Path by import external jars file which can be found in the "libs" folder
- * 3.Create/copy "IPAddress.txt" into the project workspace
- * 4.Edit the IP Address in the text file
- * 5.Define the BinFilePath in line 73
- */
- try{
- //Read IP addresses into array from the textFile
- List<String> lines = new ArrayList<String>();
- while(sc.hasNextLine()){
- lines.add(sc.nextLine());
- }
- try
- {
- //Define the path of the file here
- // Example : String BinFilePath = "C:/Users/xxx/Desktop/ip2locationjavacomponent/demos/IP2LOCATION-LITE-DB1.BIN";
- //Define the IP Address you want to test
- IP2Location loc = new IP2Location();
- if (args.length == 2 || args.length == 3)
- {
- loc.IPDatabasePath = args[0];
- if (args.length == 3)
- {
- loc.IPLicensePath = args[2];
- }
- IPResult rec = loc.IPQuery(args[1]);
- if ("OK".equals(rec.getStatus()))
- {
- }
- else if ("EMPTY_IP_ADDRESS".equals(rec.getStatus()))
- {
- }
- else if ("INVALID_IP_ADDRESS".equals(rec.getStatus()))
- {
- }
- else if ("MISSING_FILE".equals(rec.getStatus()))
- {
- }
- else if ("IPV6_NOT_SUPPORTED".equals(rec.getStatus()))
- {
- }
- else
- {
- }
- if (rec.getDelay() == true)
- {
- }
- }
- else
- {
- }
- }
- {
- }
- }
- }
- }
- }
- }
Add new comment
- 373 views