使用 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

相关文章

SpringBoot 获取请求参数的常用注解及用法

《SpringBoot获取请求参数的常用注解及用法》SpringBoot通过@RequestParam、@PathVariable等注解支持从HTTP请求中获取参数,涵盖查询、路径、请求体、头、C... 目录SpringBoot 提供了多种注解来方便地从 HTTP 请求中获取参数以下是主要的注解及其用法:1

sky-take-out项目中Redis的使用示例详解

《sky-take-out项目中Redis的使用示例详解》SpringCache是Spring的缓存抽象层,通过注解简化缓存管理,支持Redis等提供者,适用于方法结果缓存、更新和删除操作,但无法实现... 目录Spring Cache主要特性核心注解1.@Cacheable2.@CachePut3.@Ca

C#下Newtonsoft.Json的具体使用

《C#下Newtonsoft.Json的具体使用》Newtonsoft.Json是一个非常流行的C#JSON序列化和反序列化库,它可以方便地将C#对象转换为JSON格式,或者将JSON数据解析为C#对... 目录安装 Newtonsoft.json基本用法1. 序列化 C# 对象为 JSON2. 反序列化

RabbitMQ 延时队列插件安装与使用示例详解(基于 Delayed Message Plugin)

《RabbitMQ延时队列插件安装与使用示例详解(基于DelayedMessagePlugin)》本文详解RabbitMQ通过安装rabbitmq_delayed_message_exchan... 目录 一、什么是 RabbitMQ 延时队列? 二、安装前准备✅ RabbitMQ 环境要求 三、安装延时队

Python ORM神器之SQLAlchemy基本使用完全指南

《PythonORM神器之SQLAlchemy基本使用完全指南》SQLAlchemy是Python主流ORM框架,通过对象化方式简化数据库操作,支持多数据库,提供引擎、会话、模型等核心组件,实现事务... 目录一、什么是SQLAlchemy?二、安装SQLAlchemy三、核心概念1. Engine(引擎)

Java Stream 并行流简介、使用与注意事项小结

《JavaStream并行流简介、使用与注意事项小结》Java8并行流基于StreamAPI,利用多核CPU提升计算密集型任务效率,但需注意线程安全、顺序不确定及线程池管理,可通过自定义线程池与C... 目录1. 并行流简介​特点:​2. 并行流的简单使用​示例:并行流的基本使用​3. 配合自定义线程池​示

GO语言中函数命名返回值的使用

《GO语言中函数命名返回值的使用》在Go语言中,函数可以为其返回值指定名称,这被称为命名返回值或命名返回参数,这种特性可以使代码更清晰,特别是在返回多个值时,感兴趣的可以了解一下... 目录基本语法函数命名返回特点代码示例命名特点基本语法func functionName(parameters) (nam

使用shardingsphere实现mysql数据库分片方式

《使用shardingsphere实现mysql数据库分片方式》本文介绍如何使用ShardingSphere-JDBC在SpringBoot中实现MySQL水平分库,涵盖分片策略、路由算法及零侵入配置... 目录一、ShardingSphere 简介1.1 对比1.2 核心概念1.3 Sharding-Sp

Java 正则表达式的使用实战案例

《Java正则表达式的使用实战案例》本文详细介绍了Java正则表达式的使用方法,涵盖语法细节、核心类方法、高级特性及实战案例,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要... 目录一、正则表达式语法详解1. 基础字符匹配2. 字符类([]定义)3. 量词(控制匹配次数)4. 边

Python Counter 函数使用案例

《PythonCounter函数使用案例》Counter是collections模块中的一个类,专门用于对可迭代对象中的元素进行计数,接下来通过本文给大家介绍PythonCounter函数使用案例... 目录一、Counter函数概述二、基本使用案例(一)列表元素计数(二)字符串字符计数(三)元组计数三、C