DEV Community

Mohammed Riyas
Mohammed Riyas

Posted on


Ip Location tracker using Java Spring and Geolite2 database

Before get into code you need first download GeoLite2 database. i drop link down. once you enter the GeoLite2 page you need signup and then only you can able to download the database. After click the download link, download page will open in that page you need to download Geolite2-city-20220201.tar.gz file. After download you need to extract that downloaded file using some gz file extracting app. then all set lets get back to code.

In Pom.xml you need to add one dependency only maxmind geoip pls check my pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="" xmlns:xsi=""
        <relativePath/> <!-- lookup parent from repository -->
    <description>Riyasproject for Spring Boot</description>
<!-- -->




Enter fullscreen mode Exit fullscreen mode
package com.example.demo;


import org.springframework.boot.autoconfigure.SpringBootApplication;

import com.maxmind.geoip2.DatabaseReader;
import com.maxmind.geoip2.exception.GeoIp2Exception;
import com.maxmind.geoip2.model.CityResponse;

public class IpLocationApplication {

    public static void main(String[] args) throws IOException, GeoIp2Exception {
              // add golite2 database downloaded location
        String dblocation="D:\\GeoLite2-City_20220201\\GeoLite2-City.mmdb";
        File f=new File(dblocation);
        DatabaseReader database=new DatabaseReader.Builder(f).build();
//to get that ip address pls google search 'my ip' you will get your own ip address
        InetAddress ip=InetAddress.getByName("2409:4073:4d87:f2a:11bc:4641:f738:79aa");
    String country= response.getCountry().getName();
    String city=response.getCity().getName();
double lat= response.getLocation().getLatitude();
double log= response.getLocation().getLongitude();
    String postal=response.getPostal().getCode();
    String state=response.getLeastSpecificSubdivision().getName();
    System.out.println("country = "+country);
    System.out.println("state = "+state);
    System.out.println("city = "+city);
    System.out.println("postal = "+postal);
    System.out.println("lat = "+lat);
    System.out.println("log = "+log);


Enter fullscreen mode Exit fullscreen mode

if you have any queries pls feel free to contact

Top comments (0)

12 APIs That You Will Love

Free and easy to use APIs for your next project, learning a new technology, or building a new feature.