当前位置: 首页 > news >正文

使用ip2region获取客户端地区

目录

从gitee拉取ip2region.xdb资源文件

 写测试类

注意要写对资源路径

 本地测试结果

​编辑 远端测试结果


从gitee拉取ip2region.xdb资源文件

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

 将xdb放入resources资源文件夹

引入依赖 

<dependency><groupId>org.lionsoul</groupId><artifactId>ip2region</artifactId><version>2.7.0</version>
</dependency>

 

写测试类

     private Searcher searcher;@GetMapping("test")@ApiOperation("test")public String test() throws IOException  {HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();String ipAddress = null;try {// 获取请求客户端的ipipAddress = request.getHeader("x-forwarded-for");if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {ipAddress = request.getHeader("Proxy-Client-IP");}if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {ipAddress = request.getHeader("WL-Proxy-Client-IP");}if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {ipAddress = request.getRemoteAddr();if (ipAddress.equals("127.0.0.1")||ipAddress.equals("0:0:0:0:0:0:0:1")) {ipAddress = "127.0.0.1";}}// 判断ip是否符合规格if (ipAddress != null && ipAddress.length() > 15) { // "***.***.***.***".length()// = 15if (ipAddress.indexOf(",") > 0) {ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));}}} catch (Exception e) {ipAddress="";}if ("127.0.0.1".equals(ipAddress) || ipAddress.startsWith("192.168")) {return "局域网 ip";}String dbPath;if (searcher == null) {try {// 加载ip2region 文件searcher=Searcher.newWithFileOnly("pipayshop-api/src/main/resources/ipdb/ip2region.xdb");} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}String region = null;String errorMessage = null;try {// 获取地区region = searcher.search(ipAddress);} catch (Exception e) {errorMessage = e.getMessage();if (errorMessage != null && errorMessage.length() > 256) {errorMessage = errorMessage.substring(0,256);}e.printStackTrace();}// 输出 regionreturn region;}

注意要写对资源路径

本地的资源路径

远端服务器资源路径(需要与你写的路径一一对应,不然找不到文件)

 本地测试结果

 远端测试结果

http://www.lryc.cn/news/125899.html

相关文章:

  • RH850从0搭建Autosar开发环境【24】- Davinci Configurator之DEM模块配置详解(上)
  • uniapp封装接口
  • Android布局【TableLayout】
  • C/C++中static关键字详解
  • Memory Analyzer(MAT)分析内存
  • 计算机网络面试题
  • 【LeetCode75】第三十题 奇偶链表
  • docker 学习--03 环境安装(本人使用的win10 Linux也是在win10下模拟)
  • 【代码质量】认知复杂度(COGNITIVE COMPLEXITY)一种衡量可理解性的新方法
  • 什么是JavaScript中的内存泄漏和如何避免内存泄漏?
  • 安全头响应头(三)​X-Content-Type-Options
  • 13 计算机视觉-代码详解
  • jupyter打开ipynb后,还没有运行cell,反复报错
  • 一台阿里云服务器怎么部署多个网站?以CentOS系统为例
  • history记录日期时间和日志记录操作
  • RocketMQ 单机源码部署 自定义配置文件和端口以及acl权限配置解析
  • NuGet控制台命令初步使用
  • 2023年国赛数学建模思路 - 案例:FPTree-频繁模式树算法
  • Positive Technologies:有针对性的攻击占非洲所有攻击的 68%
  • Flink CDC系列之:TiDB CDC 导入 Elasticsearch
  • 未来混合动力汽车的发展:技术探索与前景展望
  • C进阶(2/7)前篇——指针进阶
  • C 内存分配器 mimalloc
  • leetcode做题笔记74搜索二维矩阵
  • 深信服数据中心管理系统 XXE漏洞复现
  • 【Kubernetes】Kubernetes的Pod进阶
  • 都错了!机械硬盘远比SSD更省电 最多领先94%
  • tomcat设置PermSize
  • JVM——分代收集理论和垃圾回收算法
  • jar包独立运行的几种方式