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.

https://dev.maxmind.com/geoip/geolite2-free-geolocation-data?lang=en

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

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.IpLocation</groupId>
    <artifactId>IpLocation</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>IpLocation</name>
    <description>Riyasproject for Spring Boot</description>
    <properties>
        <java.version>11</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
<!-- https://mvnrepository.com/artifact/com.maxmind.geoip2/geoip2 -->
<dependency>
    <groupId>com.maxmind.geoip2</groupId>
    <artifactId>geoip2</artifactId>
    <version>3.0.0</version>
</dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

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


import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;

import org.springframework.boot.autoconfigure.SpringBootApplication;

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

@SpringBootApplication
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");
        CityResponse response=database.city(ip);
    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
muhammedriyas6262@gmail.com

Oldest comments (0)