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

    技术2022-07-10  125

    原链接:https://blog.csdn.net/weixin_42144379/article/details/84900446

    首先在 maven 里面引入 ip2region :

    <dependency>     <groupId>org.lionsoul</groupId>     <artifactId>ip2region</artifactId>     <version>1.7</version> </dependency>  然后下载  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

    Processed: 0.010, SQL: 9