JAVA根据ip地址获取归属地的实现方法
IP获取归属地
1.通过地址库获取
如果使用API接口获取,可能会出现服务挂了,或者服务地址不提供服务了等问题。而采用本地地址库就没有这些问题。
本文采用离线IP地址定位库 Ip2region,Ip2region是一个离线IP地址定位库,微秒的查询时间:
实现步骤:
访问官网github地址:https://github.com/lionsoul2014/ip2region

找到data目录下的:ip2region.xdb文件下载下来

把ip2region.xdb文件放在resources目录下

在模块中引入maven依赖
<dependency> <groupId>org.lionsoul</groupId> <artifactId>ip2region</artifactId> <version>2.6.5</version> </dependency>
获取归属地:
private Searcher searcher;
@Override
public String getIpAddress(String ip){
if ("127.0.0.1".equals(ip) || ip.startsWith("192.168")) {
return "|||局域网ip";
}
if (searcher == null) {
try {
File file = ResourceUtils.getFile("classpath:db/data_ip2region.xdb");
String dbPath = file.getPath();
searcher = Searcher.newWithFileOnly(dbPath);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
String region = null;
String errorMessage = null;
try {
region = searcher.search(ip);
} catch (Exception e) {
errorMessage = e.getMessage();
if (errorMessage != null && errorMessage.length() > 256) {
errorMessage = errorMessage.substring(0, 256);
}
e.printStackTrace();
}
// 输出 region
System.out.println(region);
return region;
}
这里Searcher引用的是仓库里面的检索方法,到这里就完成了可以获取到ip对应的归属地。
调用方法后运行结果如下

注意!!!! 本地是没问题的 如果打成jar包放在linux服务器上会读取不到
解决办法:
public String getIpCity(String ip) {
if ("127.0.0.1".equals(ip) || ip.startsWith("192.168")) {
return "|||局域网ip";
}
if (searcher == null) {
try {
//本地环境需要加上 classpath:
// File file = ResourceUtils.getFile("db/data_ip2region.xdb");
// String dbPath = file.getPath();
// searcher = Searcher.newWithFileOnly(dbPath);
//这里通过流获取 解决jar包无法读取文件问题
ResponseEntity<byte[]> test = test("db/data_ip2region.xdb");
searcher = Searcher.newWithBuffer(test.getBody());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
String region = null;
String errorMessage = null;
try {
region = searcher.search(ip);
} catch (Exception e) {
errorMessage = e.getMessage();
if (errorMessage != null && errorMessage.length() > 256) {
errorMessage = errorMessage.substring(0, 256);
}
e.printStackTrace();
}
// 输出 region
return region;
}
public static ResponseEntity<byte[]> test(String templateName) throws IOException {
ClassPathResource classPathResource = new ClassPathResource(templateName);
String filename = classPathResource.getFilename();
@Cleanup InputStream inputStream = classPathResource.getInputStream();
byte[] bytes = FileCopyUtils.copyToByteArray(inputStream);
String fileName = new String(filename.getBytes("UTF-8"), "iso-8859-1");// 为了解决中文名称乱码问题
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
headers.setContentDispositionFormData("attachment", fileName);
return new ResponseEntity<>(bytes, headers, HttpStatus.CREATED);
}至于什么是Ip2region 官网上面有介绍这里就不多介绍了
到此这篇关于JAVA根据ip地址获取归属地的实现方法的文章就介绍到这了,更多相关JAVA ip地址获取归属地内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
SpringBoot集成SAP的过程及本地IDEA启动和Windows服务器部署
本文给大家介绍SpringBoot集成SAP的过程及本地IDEA启动和Windows服务器部署的相关知识,本文给大家介绍的详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧2025-07-07
Java SpringBoot详解集成以及配置Swagger流程
Swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。总体目标是使客户端和文件系统作为服务器以同样的速度来更新。文件的方法,参数和模型紧密集成到服务器端的代码,允许API来始终保持同步2021-10-10


最新评论