使用 ip2region 或 geoip2 根据用户ip获取用户省市

2024-01-28 23:36

本文主要是介绍使用 ip2region 或 geoip2 根据用户ip获取用户省市,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

使用 ip2region 或 geoip2 根据用户ip获取用户省市

这两种方式都不能获取到区县级别
简单的测试 中感觉 geoip 不如 ip2region 精确,而且 ip2region 的数据库文件也要小一些

ip2region

官方地址: https://github.com/lionsoul2014/ip2region/tree/master

在pom中引入内容

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

代码示例

import org.lionsoul.ip2region.xdb.Searcher;
public String testIp2Region() {  // 测试的ip地址  String ip = "112.13.53.138";  // 获取resource下的xdb文件  String dbPath = Object.class.getResource("/ip2region.xdb").getPath();  // 1、从 dbPath 加载整个 xdb 到内存。  byte[] cBuff;  try {  cBuff = Searcher.loadContentFromFile(dbPath);  } catch (Exception e) {  System.out.printf("failed to load content from `%s`: %s\n", dbPath, e);  return null;    }  // 2、使用上述的 cBuff 创建一个完全基于内存的查询对象。  Searcher searcher;  try {  searcher = Searcher.newWithBuffer(cBuff);  } catch (Exception e) {  System.out.printf("failed to create content cached searcher: %s\n", e);  return null;    } // 3、查询  try {// 结果的固定格式 国家|区域|省份|城市|ISP 缺省补0// 这里区域没有获取到 补了0String region = searcher.search(ip);System.out.printf(region); // 中国|0|浙江省|温州市|移动  return region;  } catch (Exception e) {  System.out.printf("failed to search(%s): %s\n", ip, e);  }  return null;  
}

geoip2

官方下载地址需要注册: https://www.maxmind.com/en/accounts/current/geoip/downloads

我是在这里下载的: https://github.com/P3TERX/GeoLite.mmdb?tab=readme-ov-file

在pom中引入内容

<dependency>  <groupId>com.maxmind.geoip2</groupId>  <artifactId>geoip2</artifactId>  <version>2.12.0</version>  
</dependency>

代码示例

import com.maxmind.geoip2.DatabaseReader;  
import com.maxmind.geoip2.model.CityResponse;  
import com.maxmind.geoip2.record.*;
public void testGeoip() {  // 测试ip  String ip = "112.13.53.138";  // GeoIP2-City 数据库文件  String dbPath = Object.class.getResource("/GeoLite2-City.mmdb").getPath();  File database = new File(dbPath);  // 创建 DatabaseReader对象  try {  DatabaseReader reader = new DatabaseReader.Builder(database).build();  InetAddress ipAddress = InetAddress.getByName(ip);  CityResponse response = reader.city(ipAddress);  Country country = response.getCountry();  System.out.println(country.getIsoCode());  System.out.println(country.getName()); // 国家  System.out.println(country.getNames().get("zh-CN")); // 国家中文  Subdivision subdivision = response.getMostSpecificSubdivision();  System.out.println(subdivision.getIsoCode());  System.out.println(subdivision.getName()); // 省市  System.out.println(subdivision.getNames().get("zh-CN"));// 省市中文  City city = response.getCity();  System.out.println(city.getName()); // 城市  System.out.println(city.getNames().get("zh-CN"));// 城市中文  Location location = response.getLocation();  System.out.println(location.getLatitude());  // 纬度  System.out.println(location.getLongitude()); // 经度  } catch (Exception e) {  e.printStackTrace();  }  
}

这篇关于使用 ip2region 或 geoip2 根据用户ip获取用户省市的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/655139

相关文章

Python数据验证神器Pydantic库的使用和实践中的避坑指南

《Python数据验证神器Pydantic库的使用和实践中的避坑指南》Pydantic是一个用于数据验证和设置的库,可以显著简化API接口开发,文章通过一个实际案例,展示了Pydantic如何在生产环... 目录1️⃣ 崩溃时刻:当你的API接口又双叒崩了!2️⃣ 神兵天降:3行代码解决验证难题3️⃣ 深度

Linux内核定时器使用及说明

《Linux内核定时器使用及说明》文章详细介绍了Linux内核定时器的特性、核心数据结构、时间相关转换函数以及操作API,通过示例展示了如何编写和使用定时器,包括按键消抖的应用... 目录1.linux内核定时器特征2.Linux内核定时器核心数据结构3.Linux内核时间相关转换函数4.Linux内核定时

python中的flask_sqlalchemy的使用及示例详解

《python中的flask_sqlalchemy的使用及示例详解》文章主要介绍了在使用SQLAlchemy创建模型实例时,通过元类动态创建实例的方式,并说明了如何在实例化时执行__init__方法,... 目录@orm.reconstructorSQLAlchemy的回滚关联其他模型数据库基本操作将数据添

Spring配置扩展之JavaConfig的使用小结

《Spring配置扩展之JavaConfig的使用小结》JavaConfig是Spring框架中基于纯Java代码的配置方式,用于替代传统的XML配置,通过注解(如@Bean)定义Spring容器的组... 目录JavaConfig 的概念什么是JavaConfig?为什么使用 JavaConfig?Jav

Java使用Spire.Doc for Java实现Word自动化插入图片

《Java使用Spire.DocforJava实现Word自动化插入图片》在日常工作中,Word文档是不可或缺的工具,而图片作为信息传达的重要载体,其在文档中的插入与布局显得尤为关键,下面我们就来... 目录1. Spire.Doc for Java库介绍与安装2. 使用特定的环绕方式插入图片3. 在指定位

Springboot3 ResponseEntity 完全使用案例

《Springboot3ResponseEntity完全使用案例》ResponseEntity是SpringBoot中控制HTTP响应的核心工具——它能让你精准定义响应状态码、响应头、响应体,相比... 目录Spring Boot 3 ResponseEntity 完全使用教程前置准备1. 项目基础依赖(M

springboot的controller中如何获取applicatim.yml的配置值

《springboot的controller中如何获取applicatim.yml的配置值》本文介绍了在SpringBoot的Controller中获取application.yml配置值的四种方式,... 目录1. 使用@Value注解(最常用)application.yml 配置Controller 中

Java使用Spire.Barcode for Java实现条形码生成与识别

《Java使用Spire.BarcodeforJava实现条形码生成与识别》在现代商业和技术领域,条形码无处不在,本教程将引导您深入了解如何在您的Java项目中利用Spire.Barcodefor... 目录1. Spire.Barcode for Java 简介与环境配置2. 使用 Spire.Barco

Android使用java实现网络连通性检查详解

《Android使用java实现网络连通性检查详解》这篇文章主要为大家详细介绍了Android使用java实现网络连通性检查的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录NetCheck.Java(可直接拷贝)使用示例(Activity/Fragment 内)权限要求

C# 预处理指令(# 指令)的具体使用

《C#预处理指令(#指令)的具体使用》本文主要介绍了C#预处理指令(#指令)的具体使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学... 目录1、预处理指令的本质2、条件编译指令2.1 #define 和 #undef2.2 #if, #el