hard0zi
Blockchain Consensus Architect
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
The sample can be requested at https://www.ip2location.com/software/java-component
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
- 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(
)
- {
- }
- public
static
void
main(
String
[
]
args)
- {
- /*
- * 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
- */
- String
filename =
"IpAddress.txt"
;
- try
{
- //Read IP addresses into array from the textFile
- Scanner sc =
new
Scanner(
new
File
(
filename)
)
;
- List<
String>
lines =
new
ArrayList<
String>
(
)
;
- while
(
sc.hasNextLine
(
)
)
{
- lines.add
(
sc.nextLine
(
)
)
;
- }
- String
[
]
arr =
lines.toArray
(
new
String
[
0
]
)
;
- for
(
String
line:
arr)
{
- try
- {
- //Define the path of the file here
- // Example : String BinFilePath = "C:/Users/xxx/Desktop/ip2locationjavacomponent/demos/IP2LOCATION-LITE-DB1.BIN";
- String
BinFilePath =
""
;
- //Define the IP Address you want to test
- String
ipAddress =
line;
- args =
new
String
[
]
{
BinFilePath ,ipAddress}
;
- 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
(
)
)
)
- {
- System
.out
.println
(
rec)
;
- }
- else
if
(
"EMPTY_IP_ADDRESS"
.equals
(
rec.getStatus
(
)
)
)
- {
- System
.out
.println
(
"IP address cannot be blank."
)
;
- }
- else
if
(
"INVALID_IP_ADDRESS"
.equals
(
rec.getStatus
(
)
)
)
- {
- System
.out
.println
(
"Invalid IP address."
)
;
- }
- else
if
(
"MISSING_FILE"
.equals
(
rec.getStatus
(
)
)
)
- {
- System
.out
.println
(
"Invalid database path."
)
;
- }
- else
if
(
"IPV6_NOT_SUPPORTED"
.equals
(
rec.getStatus
(
)
)
)
- {
- System
.out
.println
(
"This BIN does not contain IPv6 data."
)
;
- }
- else
- {
- System
.out
.println
(
"Unknown error."
+
rec.getStatus
(
)
)
;
- }
- if
(
rec.getDelay
(
)
==
true
)
- {
- System
.out
.println
(
"The last query was delayed for 5 seconds because this is an evaluation copy."
)
;
- }
- System
.out
.println
(
"Java Component: "
+
rec.getVersion
(
)
)
;
- }
- else
- {
- System
.out
.println
(
"Usage: Main <IPDatabasePath> <IPAddress> [IPLicensePath]"
)
;
- System
.out
.println
(
" "
)
;
- System
.out
.println
(
" <IPDatabasePath> Specify BIN data file"
)
;
- System
.out
.println
(
" <IPAddress> Specify IP address"
)
;
- System
.out
.println
(
" [IPLicensePath] Path of registration license file (optional)"
)
;
- System
.out
.println
(
" * Please leave this field empty for unregistered version."
)
;
- System
.out
.println
(
" "
)
;
- System
.out
.println
(
"URL: http://www.ip2location.com"
)
;
- }
- }
- catch
(
Exception
e)
- {
- System
.out
.println
(
e)
;
- e.printStackTrace
(
System
.out
)
;
- }
- }
- }
- catch
(
IOException
ioE)
{
- System
.out
.println
(
"Unable read the file!!"
)
;
- System
.out
.println
(
ioE.getMessage
(
)
)
;
- }
- }
- }
The sample can be requested at https://www.ip2location.com/software/java-component