Query IP Address Location in Bulk using Java

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
  1. import com.ip2location.*;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.util.Scanner;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7.  
  8. public class Main
  9. {
  10. public Main()
  11. {
  12. }
  13.  
  14. public static void main(String[] args)
  15. {
  16. /*
  17. * Steps:
  18. * 1.Copy the Main.java from the "demos" folder
  19. * 2.Configure Build Path by import external jars file which can be found in the "libs" folder
  20. * 3.Create/copy "IPAddress.txt" into the project workspace
  21. * 4.Edit the IP Address in the text file
  22. * 5.Define the BinFilePath in line 73
  23. */
  24.  
  25. String filename = "IpAddress.txt";
  26.  
  27. try{
  28. //Read IP addresses into array from the textFile
  29. Scanner sc = new Scanner(new File(filename));
  30. List<String> lines = new ArrayList<String>();
  31. while(sc.hasNextLine()){
  32. lines.add(sc.nextLine());
  33. }
  34. String[] arr = lines.toArray(new String[0]);
  35.  
  36. for(String line: arr){
  37. try
  38. {
  39. //Define the path of the file here
  40. // Example : String BinFilePath = "C:/Users/xxx/Desktop/ip2locationjavacomponent/demos/IP2LOCATION-LITE-DB1.BIN";
  41. String BinFilePath = "";
  42.  
  43. //Define the IP Address you want to test
  44. String ipAddress = line;
  45.  
  46. args = new String[]{BinFilePath ,ipAddress};
  47. IP2Location loc = new IP2Location();
  48. if (args.length == 2 || args.length == 3)
  49. {
  50. loc.IPDatabasePath = args[0];
  51. if (args.length == 3)
  52. {
  53. loc.IPLicensePath = args[2];
  54. }
  55. IPResult rec = loc.IPQuery(args[1]);
  56. if ("OK".equals(rec.getStatus()))
  57. {
  58. System.out.println(rec);
  59. }
  60. else if ("EMPTY_IP_ADDRESS".equals(rec.getStatus()))
  61. {
  62. System.out.println("IP address cannot be blank.");
  63. }
  64. else if ("INVALID_IP_ADDRESS".equals(rec.getStatus()))
  65. {
  66. System.out.println("Invalid IP address.");
  67. }
  68. else if ("MISSING_FILE".equals(rec.getStatus()))
  69. {
  70. System.out.println("Invalid database path.");
  71. }
  72. else if ("IPV6_NOT_SUPPORTED".equals(rec.getStatus()))
  73. {
  74. System.out.println("This BIN does not contain IPv6 data.");
  75. }
  76. else
  77. {
  78. System.out.println("Unknown error." + rec.getStatus());
  79. }
  80. if (rec.getDelay() == true)
  81. {
  82. System.out.println("The last query was delayed for 5 seconds because this is an evaluation copy.");
  83. }
  84. System.out.println("Java Component: " + rec.getVersion());
  85. }
  86. else
  87. {
  88. System.out.println("Usage: Main <IPDatabasePath> <IPAddress> [IPLicensePath]");
  89. System.out.println(" ");
  90. System.out.println(" <IPDatabasePath> Specify BIN data file");
  91. System.out.println(" <IPAddress> Specify IP address");
  92. System.out.println(" [IPLicensePath] Path of registration license file (optional)");
  93. System.out.println(" * Please leave this field empty for unregistered version.");
  94. System.out.println(" ");
  95. System.out.println("URL: http://www.ip2location.com");
  96. }
  97. }
  98. catch(Exception e)
  99. {
  100. System.out.println(e);
  101. e.printStackTrace(System.out);
  102. }
  103. }
  104.  
  105.  
  106.  
  107. }
  108. catch(IOException ioE){
  109. System.out.println("Unable read the file!!");
  110. System.out.println(ioE.getMessage());
  111. }
  112.  
  113. }
  114. }
The sample can be requested at https://www.ip2location.com/software/java-component

Add new comment