-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetLocationExample.java
More file actions
executable file
·54 lines (39 loc) · 1.63 KB
/
GetLocationExample.java
File metadata and controls
executable file
·54 lines (39 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package com.mkyong.analysis.location;
import java.io.File;
import java.io.IOException;
import com.maxmind.geoip.Location;
import com.maxmind.geoip.LookupService;
import com.maxmind.geoip.regionName;
import com.mkyong.analysis.location.mode.ServerLocation;
public class GetLocationExample {
public static void main(String[] args) {
GetLocationExample obj = new GetLocationExample();
ServerLocation location = obj.getLocation("206.190.36.45");
System.out.println(location);
}
public ServerLocation getLocation(String ipAddress) {
File file = new File(
"C:\\resources\\location\\GeoLiteCity.dat");
return getLocation(ipAddress, file);
}
public ServerLocation getLocation(String ipAddress, File file) {
ServerLocation serverLocation = null;
try {
serverLocation = new ServerLocation();
LookupService lookup = new LookupService(file,LookupService.GEOIP_MEMORY_CACHE);
Location locationServices = lookup.getLocation(ipAddress);
serverLocation.setCountryCode(locationServices.countryCode);
serverLocation.setCountryName(locationServices.countryName);
serverLocation.setRegion(locationServices.region);
serverLocation.setRegionName(regionName.regionNameByCode(
locationServices.countryCode, locationServices.region));
serverLocation.setCity(locationServices.city);
serverLocation.setPostalCode(locationServices.postalCode);
serverLocation.setLatitude(String.valueOf(locationServices.latitude));
serverLocation.setLongitude(String.valueOf(locationServices.longitude));
} catch (IOException e) {
System.err.println(e.getMessage());
}
return serverLocation;
}
}