0

I am looking a way to get the region from the given IP address.I found that there are plenty of Apis available on the internet. Eg:GeoLite but the problem was that my IP adresses are resides in private network so those are not visible for publicly.My question is that is there any native way to achieve this on different platforms(Linux,Windows) using Java.

Get IP adress from Java

InetAddress addr = InetAddress.getLocalHost();
 System.out.println(addr.getHostAddress());

GeoLite code snippet

LookupService lookup =new LookupService("src\\main\\resources\\GeoLocationDB\\GeoLiteCity.dat",LookupService.GEOIP_CHECK_CACHE);
     Location locationServices = lookup.getLocation("10.xx.xx.xxx");

    System.out.println(locationServices.city);//throws a null 
gihan-maduranga
  • 4,381
  • 5
  • 41
  • 74
  • 1) You need to find the IP address of the machine/router/etc. which provide you an access to the internet. For this IP address the region can be narrowed. If your internet connection point is in a different region than your PC, this would provide the wrong region for your machine. 2) To find the region for a machine with a private network address this is impossible. Except your machine has some other way to get the region, e.g. a GPS receiver. – SubOptimal Jan 09 '17 at 07:18
  • @SubOptimal thanks for quick reply. i updated post with how i collected the IP from machine.But it gives me private ip adress and it wont visible for publicly. – gihan-maduranga Jan 09 '17 at 08:01
  • 1
    You can retrieve your external IP address like explained here https://stackoverflow.com/questions/2939218/getting-the-external-ip-address-in-java#2939223 – SubOptimal Jan 09 '17 at 08:12

0 Answers0