• We just launched and are currently in beta. Join us as we build and grow the community.

Query IP Address Location in Bulk using Java

hard0zi

Blockchain Consensus Architect
H Rep
0
0
0
Rep
0
H Vouches
0
0
0
Vouches
0
Posts
67
Likes
119
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
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. public

    class

    Main
  8. {
  9. public

    Main(

    )
  10. {
  11. }

  12. public

    static

    void

    main(

    String

    [

    ]

    args)
  13. {
  14. /*
  15. * Steps:
  16. * 1.Copy the Main.java from the "demos" folder
  17. * 2.Configure Build Path by import external jars file which can be found in the "libs" folder
  18. * 3.Create/copy "IPAddress.txt" into the project workspace
  19. * 4.Edit the IP Address in the text file
  20. * 5.Define the BinFilePath in line 73
  21. */

  22. String

    filename =

    "IpAddress.txt"

    ;

  23. try

    {
  24. //Read IP addresses into array from the textFile
  25. Scanner sc =

    new

    Scanner(

    new

    File

    (

    filename)

    )

    ;
  26. List<

    String>

    lines =

    new

    ArrayList<

    String>

    (

    )

    ;
  27. while

    (

    sc.hasNextLine

    (

    )

    )

    {
  28. lines.add

    (

    sc.nextLine

    (

    )

    )

    ;
  29. }
  30. String

    [

    ]

    arr =

    lines.toArray

    (

    new

    String

    [

    0

    ]

    )

    ;

  31. for

    (

    String

    line:

    arr)

    {
  32. try
  33. {
  34. //Define the path of the file here
  35. // Example : String BinFilePath = "C:/Users/xxx/Desktop/ip2locationjavacomponent/demos/IP2LOCATION-LITE-DB1.BIN";
  36. String

    BinFilePath =

    ""

    ;

  37. //Define the IP Address you want to test
  38. String

    ipAddress =

    line;

  39. args =

    new

    String

    [

    ]

    {

    BinFilePath ,ipAddress}

    ;
  40. IP2Location loc =

    new

    IP2Location(

    )

    ;
  41. if

    (

    args.length

    ==

    2

    ||

    args.length

    ==

    3

    )
  42. {
  43. loc.IPDatabasePath

    =

    args[

    0

    ]

    ;
  44. if

    (

    args.length

    ==

    3

    )
  45. {
  46. loc.IPLicensePath

    =

    args[

    2

    ]

    ;
  47. }
  48. IPResult rec =

    loc.IPQuery

    (

    args[

    1

    ]

    )

    ;
  49. if

    (

    "OK"

    .equals

    (

    rec.getStatus

    (

    )

    )

    )
  50. {
  51. System

    .out

    .println

    (

    rec)

    ;
  52. }
  53. else

    if

    (

    "EMPTY_IP_ADDRESS"

    .equals

    (

    rec.getStatus

    (

    )

    )

    )
  54. {
  55. System

    .out

    .println

    (

    "IP address cannot be blank."

    )

    ;
  56. }
  57. else

    if

    (

    "INVALID_IP_ADDRESS"

    .equals

    (

    rec.getStatus

    (

    )

    )

    )
  58. {
  59. System

    .out

    .println

    (

    "Invalid IP address."

    )

    ;
  60. }
  61. else

    if

    (

    "MISSING_FILE"

    .equals

    (

    rec.getStatus

    (

    )

    )

    )
  62. {
  63. System

    .out

    .println

    (

    "Invalid database path."

    )

    ;
  64. }
  65. else

    if

    (

    "IPV6_NOT_SUPPORTED"

    .equals

    (

    rec.getStatus

    (

    )

    )

    )
  66. {
  67. System

    .out

    .println

    (

    "This BIN does not contain IPv6 data."

    )

    ;
  68. }
  69. else
  70. {
  71. System

    .out

    .println

    (

    "Unknown error."

    +

    rec.getStatus

    (

    )

    )

    ;
  72. }
  73. if

    (

    rec.getDelay

    (

    )

    ==

    true

    )
  74. {
  75. System

    .out

    .println

    (

    "The last query was delayed for 5 seconds because this is an evaluation copy."

    )

    ;
  76. }
  77. System

    .out

    .println

    (

    "Java Component: "

    +

    rec.getVersion

    (

    )

    )

    ;
  78. }
  79. else
  80. {
  81. System

    .out

    .println

    (

    "Usage: Main <IPDatabasePath> <IPAddress> [IPLicensePath]"

    )

    ;
  82. System

    .out

    .println

    (

    " "

    )

    ;
  83. System

    .out

    .println

    (

    " <IPDatabasePath> Specify BIN data file"

    )

    ;
  84. System

    .out

    .println

    (

    " <IPAddress> Specify IP address"

    )

    ;
  85. System

    .out

    .println

    (

    " [IPLicensePath] Path of registration license file (optional)"

    )

    ;
  86. System

    .out

    .println

    (

    " * Please leave this field empty for unregistered version."

    )

    ;
  87. System

    .out

    .println

    (

    " "

    )

    ;
  88. System

    .out

    .println

    (

    "URL: http://www.ip2location.com"

    )

    ;
  89. }
  90. }
  91. catch

    (

    Exception

    e)
  92. {
  93. System

    .out

    .println

    (

    e)

    ;
  94. e.printStackTrace

    (

    System

    .out

    )

    ;
  95. }
  96. }



  97. }
  98. catch

    (

    IOException

    ioE)

    {
  99. System

    .out

    .println

    (

    "Unable read the file!!"

    )

    ;
  100. System

    .out

    .println

    (

    ioE.getMessage

    (

    )

    )

    ;
  101. }

  102. }
  103. }

The sample can be requested at https://www.ip2location.com/software/java-component

 

452,292

323,526

323,535

Top