java离线使用ip2region获取IP归属地

1.maven引入:

<dependency>
    <groupId>net.dreamlu</groupId>
    <artifactId>mica-ip2region</artifactId>
    <version>2.5.6</version>
</dependency>

2.下载ip2region.db

下载链接:

3.工具类

import net.dreamlu.mica.ip2region.core.DbConfig;
import net.dreamlu.mica.ip2region.core.DbMakerConfigException;
import net.dreamlu.mica.ip2region.core.DbSearcher;

import java.io.FileNotFoundException;
import java.io.IOException;

public class Test {

    public static void main(String[] args) {
        String ip = "117.86.21.164";
        String db_file = "D:\work\ip2region.db";
        try {
            DbSearcher ipSearch = new DbSearcher(new DbConfig(), db_file);
            System.out.println(ipSearch.memorySearch(ip).getRegion());

        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (DbMakerConfigException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
}
经验分享 程序员 微信小程序 职场和发展