互联网技术社区|福缘小草|程序员技术博客
🏠首页
  • 开发必备
  • Java
  • Spring Boot
  • MyBatis
  • C#
  • 架构
  • 算法
  • Vue
  • JavaScript
  • HTML
  • MySQL
  • Oracle
  • SQL Server
  • PostgreSQL
  • Redis
  • MongoDB
  • ElasticSearch
  • influxDB
  • ClickHouse
  • Linux
  • Docker
  • K8s
  • 消息队列
  • Shell
  • Git
  • Nginx
  • IDEA
  • Windows
  • 安卓
  • 在线工具
  • 实用技巧
  • 开源项目
  • 好文
  • 资源
  • 网站
  • 导航
💖关于
  • 分类
  • 标签
  • 归档

baohua.yin

不会填坑的程序员不是一个好程序猿!
🏠首页
  • 开发必备
  • Java
  • Spring Boot
  • MyBatis
  • C#
  • 架构
  • 算法
  • Vue
  • JavaScript
  • HTML
  • MySQL
  • Oracle
  • SQL Server
  • PostgreSQL
  • Redis
  • MongoDB
  • ElasticSearch
  • influxDB
  • ClickHouse
  • Linux
  • Docker
  • K8s
  • 消息队列
  • Shell
  • Git
  • Nginx
  • IDEA
  • Windows
  • 安卓
  • 在线工具
  • 实用技巧
  • 开源项目
  • 好文
  • 资源
  • 网站
  • 导航
💖关于
  • 分类
  • 标签
  • 归档
  • The field file exceeds its maximum permitted size
  • SpringBoot集成ip2region2.X
    • Spring Boot
    baohua.yin
    2023-05-07
    目录

    SpringBoot集成ip2region2.X

    # SpringBoot集成ip2region2.X

    离线IP地址定位库主要用于内网或想减少对外访问http带来的资源消耗。本博客使用的是ip2region + 在线获取。

    # ip2region总体分为两个大版本:
    # 1.x:

    提供三种算法,其中内存查询速度在0.1x毫秒级别,离线库文件ip2region.db

    # 2.x:

    同样提供三种算法,其中内存查询速度在微秒级别,离线库文件ip2region.xdb

    本文使用2.X版本

    # 基本配置

    # 1、离线库下载:

    github:https://github.com/lionsoul2014/ip2region/tree/master/data

    将文件下载完成后,放到项目的resource目录下新建的ip2region目录中:

    SpringBoot集成ip2region2.X
    # 2、Maven引入

    POM文件如下:

    <!-- 离线IP地址定位库 -->
    <dependency>
        <groupId>org.lionsoul</groupId>
        <artifactId>ip2region</artifactId>
        <version>2.6.5</version>
    </dependency>
    
    <!-- 识别xdb文件 -->
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-resources-plugin</artifactId>
        <configuration>
            <encoding>UTF-8</encoding>
            <nonFilteredFileExtensions>
                <nonFilteredFileExtension>xdb</nonFilteredFileExtension>
            </nonFilteredFileExtensions>
        </configuration>
    </plugin>
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18

    # 参考项目

    ip2region:https://gitee.com/lionsoul/ip2region

    # AddressUtils工具类

    import com.alibaba.fastjson.JSONObject;
    import org.apache.commons.io.FileUtils;
    import org.apache.shiro.io.ResourceUtils;
    import org.lionsoul.ip2region.DataBlock;
    import org.lionsoul.ip2region.DbConfig;
    import org.lionsoul.ip2region.DbSearcher;
    import org.lionsoul.ip2region.Util;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    import java.io.*;
    import java.lang.reflect.Method;
    
    /**
     * @className: AddressUtils
     * @description: ip地址获取位置工具类
     * @author: 1024fuli.com
     */
    public class AddressUtils {
    
        private final static Logger LOGGER = LoggerFactory.getLogger(AddressUtils.class);
    
        public static final String IP_URL4 = "";
    
        /**
         * 根据ip获取位置
         *
         * @param ip
         * @return 国家|区域|省份|城市|ISP_
         */
        public static String getRealAddressByIp(String ip) {
            long startTime = System.currentTimeMillis();
            String address = "";
            // 内网不查询
            if (IpUtils.internalIp(ip)) {
                return "内网IP";
            }
            address = getRegion(ip);
            if (StringUtils.isEmpty(address)) {
                if (SystemConfig.isAddressEnabled()) {
                    String rspStr = HttpUtils.sendPost(IP_URL4, "ip=" + ip);
                    if (StringUtils.isEmpty(rspStr)) {
                        LOGGER.error("获取地理位置异常 {}", ip);
                        return address;
                    }
                    JSONObject obj = JSONObject.parseObject(rspStr);
                    String country = obj.getString("country");
                    String region = obj.getString("province");
                    String city = obj.getString("city");
                    String isp = obj.getString("isp");
                    if (address.equals(region) && address.equals(city)) {
                        return obj.getString("country") + obj.getString("isp") + "流量";
                    }
                    address = country + "|" + "999|" + region + "|" + city + "|" + isp;
                }
            }
            long endTime = System.currentTimeMillis();
            LOGGER.debug("获取ip位置耗时:[{}] ", endTime - startTime);
            return address;
        }
    
    
        //********************************* 根据ip离线查询地址Star ***********************************************************
        protected static Searcher searcher;
    
        static {
            InputStream resourceAsStream = AddressUtils.class.getClassLoader().getResourceAsStream("ip2region/ip2region.xdb");
            if (resourceAsStream != null) {
                byte[] cBuff;
                try {
                    cBuff = FileCopyUtils.copyToByteArray(resourceAsStream);
                    searcher = Searcher.newWithBuffer(cBuff);
                } catch (IOException e) {
                    LOGGER.error("AddressUtils-error:{}", e);
                } finally {
                    try {
                        resourceAsStream.close();
                    } catch (Exception e) {
                        LOGGER.error("AddressUtils-error:{}", e);
                    }
                }
            }
        }
    
        public static Searcher getSearch() {
            return searcher;
        }
    
        /**
         * 离线根据ip获取位置--2.X
         *
         * @param ip
         * @return 国家|区域|省份|城市|ISP_
         */
        public static String getRegion(String ip) {
            long ipLong = ipToLong(ip);
            try {
                String search = searcher.search(ipLong);
                if (StringUtils.isNotEmpty(search)) {
                    return search;
                }
            } catch (Exception e) {
                LOGGER.error("AddressUtils-error:{}", e);
            }
            return StringUtils.EMPTY;
        }
    
        //********************************* 根据ip离线查询地址End ***********************************************************
    
        /**
         * ip转long
         *
         * @param ip
         * @return long
         */
        public static long ipToLong(String ip) {
            String[] split = ip.split("\\.");
            long i1 = 0L;
            i1 += (long) Integer.parseInt(split[0]) << 24;
            i1 += (long) Integer.parseInt(split[1]) << 16;
            i1 += (long) Integer.parseInt(split[2]) << 8;
            return i1 + Integer.parseInt(split[3]);
        }
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    上次更新: 2023/08/30, 14:25:09
    The field file exceeds its maximum permitted size

    ← The field file exceeds its maximum permitted size

    最近更新
    01
    如何进行科学上网
    05-31
    02
    分享(一个外地女孩,死在了我出租的公寓)
    08-18
    03
    温家宝总理—《我的母亲》
    06-13
    更多文章>
    Copyright © 2019-2025 1024fuli.com | 本站所有资源收集整理于网络,如有侵权请发邮件联系删除。| 粤ICP备18082936号-1 | 由又拍云提供CDN支持
    • 跟随系统
    • 浅色模式
    • 深色模式
    • 阅读模式