Java 根据 IP 地址来获取 位置 -- 使用 ip2region

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_42144379/article/details/84900446
首先在 maven 里面引入 ip2region :

org.lionsoul ip2region 1.7  然后下载  IP库 ip2region.db:  https://gitee.com/lionsoul/ip2region/tree/master/data

下载过程可能需要注册 码云,码云相当于中国的 GitHub 有必要注册下.

下载解压后只需要 data 目录下的 ip2region.db 就可以了 .

把  ip2region.db 复制到 maven 项目的 resources 目录下.

然后具体实现,可以把以下代码封装成方法:

public class Ip2RegionTest {
public static void main(String[] args){
//ip
String ip=“220.248.12.158”;

       // 判断是否为IP地址 (可用)
       //boolean isIpAddress = Util.isIpAddress(ip); 

       //ip和long互转 (可用)
       //long ipLong = Util.ip2long(ip); 
       //String strIp = Util.long2ip(ipLong);

       //根据ip进行位置信息搜索
       DbConfig config = new DbConfig();

       //获取ip库的位置(放在src下)(直接通过测试类获取文件Ip2RegionTest为测试类)
       String dbfile = Ip2RegionTest.class.getResource("/ip2region.db").getPath();

       DbSearcher searcher = new DbSearcher(config, dbfile);

       //采用Btree搜索
       DataBlock block = searcher.btreeSearch(ip);

       //打印位置信息(格式:国家|大区|省份|城市|运营商)
       System.out.println(block.getRegion()); 
}

}
还有一种实现方法如下:

此内容参考了 ip2region源码的 : org.lionsoul.ip2region.test.TestSearcher.java

package com.test;

import java.io.File;
import java.lang.reflect.Method;

import org.lionsoul.ip2region.DataBlock;
import org.lionsoul.ip2region.DbConfig;
import org.lionsoul.ip2region.DbSearcher;
import org.lionsoul.ip2region.Util;

public class IPUtil {

public static String getCityInfo(String ip){

    //db
    String dbPath = IPUtil.class.getResource("/ip2region.db").getPath();

    File file = new File(dbPath);
    if ( file.exists() == false ) {
        System.out.println("Error: Invalid ip2region.db file");
    }

    //查询算法
    int algorithm = DbSearcher.BTREE_ALGORITHM; //B-tree
                    //DbSearcher.BINARY_ALGORITHM //Binary
                    //DbSearcher.MEMORY_ALGORITYM //Memory
    try {
        DbConfig config = new DbConfig();
        DbSearcher searcher = new DbSearcher(config, dbPath);

        //define the method
        Method method = null;
        switch ( algorithm )
        {
        case DbSearcher.BTREE_ALGORITHM:
            method = searcher.getClass().getMethod("btreeSearch", String.class);
            break;
        case DbSearcher.BINARY_ALGORITHM:
            method = searcher.getClass().getMethod("binarySearch", String.class);
            break;
        case DbSearcher.MEMORY_ALGORITYM:
            method = searcher.getClass().getMethod("memorySearch", String.class);
            break;
        }

        DataBlock dataBlock = null;
        if ( Util.isIpAddress(ip) == false ) {
            System.out.println("Error: Invalid ip address");
        }

        dataBlock  = (DataBlock) method.invoke(searcher, ip);

        return dataBlock.getRegion();

    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;
}


public static void main(String[] args)  throws Exception{
    System.err.println(getCityInfo("220.248.12.158"));
}

}
如果对下载后的源码 感兴趣,可以仿照此篇文章的做法:   Java - 根据IP获取城市

把 ip2region\binding\java 下的文件 复制到项目中,然后再 把 ip2region.db 复制到对应的目录

其中的 实现方法是 参考 源码中的  案例 org.lionsoul.ip2region.test.TestSearcher.java

————————————————
版权声明:本文为CSDN博主「胡萧徒」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_42144379/article/details/84900446

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值