解决获取地址错误的bug

This commit is contained in:
zykzhangyukang 2021-02-04 10:52:58 +08:00
parent 43413783ca
commit ddfc68e2ca
3 changed files with 68 additions and 45 deletions

View file

@ -1,15 +1,27 @@
## 🚀 简介 | Intro 新冠-物资管理系统 <p align="center"><img src="./document/images/logo.png" height="100" alt="logo"/></p>
<h1 align="center"> course-online &nbsp; 🚀 &nbsp; 新冠-物资管理系统 </h1>
<h3 align="center">2020新冠疫情期间, 寂寞消遣之作~</h3>
<h3 align="center"><a href="https://www.zykhome.club" target="_blank">https://www.zykhome.club</a></h3>
---
<p align="center">
<a href="https://www.oracle.com/technetwork/java/javase/downloads/index.html"><img src="https://img.shields.io/badge/JDK-8+-green.svg" alt="jdk 8+"></a>
<a href="#"><img src="https://img.shields.io/badge/license-Apache%202.0-blue.svg?longCache=true&style=flat-square"></a>
<a href="#"><img src="https://img.shields.io/badge/springboot-2.2.1-yellow.svg?style=flat-square"></a>
<a href="#"><img src="https://img.shields.io/badge/shiro-1.4.2-orange.svg?longCache=true&style=flat-square"></a>
<a href="https://www.oracle.com/technetwork/java/javase/downloads/index.html"><img src="https://img.shields.io/badge/JDK-8+-green.svg" alt="jdk 8+"></a>
<a href="https://github.com/zykzhangyukang/Xinguan"><img src="https://img.shields.io/github/stars/zykzhangyukang/Xinguan?style=social" alt="GitHub stars"></a>
<a href="https://github.com/zykzhangyukang/Xinguan"><img src="https://img.shields.io/github/forks/zykzhangyukang/Xinguan?style=social" alt="GitHub forks"></a>
<a href="https://github.com/zykzhangyukang/Xinguan"><img src="https://img.shields.io/github/repo-size/zykzhangyukang/Xinguan" alt="size"></a>
</p>
>该项目是一个以新冠疫情为主题开发的一个物资管理系统,主要分为系统模块和业务模块,业务模块是处理疫情期间,物资的发放,物资入库,以及查看物资的库存等. >该项目是一个以新冠疫情为主题开发的一个物资管理系统,主要分为系统模块和业务模块,业务模块是处理疫情期间,物资的发放,物资入库,以及查看物资的库存等.
系统模块是一个后台通用的RBAC权限模块,可以精确的控制到后台API级别的权限控制,项目不定期更新中.武汉加油~~ 系统模块是一个后台通用的RBAC权限模块,可以精确的控制到后台API级别的权限控制,项目不定期更新中.武汉加油~~
开发模式:前后端分离,前端负责数据渲染,后端返回数据。 开发模式:前后端分离,前端负责数据渲染,后端返回数据。
![https://img.shields.io/badge/license-Apache%202.0-blue.svg?longCache=true&style=flat-square](https://img.shields.io/badge/license-Apache%202.0-blue.svg?longCache=true&style=flat-square)
![https://img.shields.io/badge/springboot-2.2.1-yellow.svg?style=flat-square](https://img.shields.io/badge/springboot-2.2.1-yellow.svg?style=flat-square)
![https://img.shields.io/badge/shiro-1.4.2-orange.svg?longCache=true&style=flat-square](https://img.shields.io/badge/shiro-1.4.2-orange.svg?longCache=true&style=flat-square)
### [在线预览](https://www.zykhome.club/#/login "在线预览") & [后端项目](https://github.com/zykzhangyukang/Xinguan "后端项目") & [前端项目](https://github.com/zykzhangyukang/xinguan-vue "前端项目") ### [在线预览](https://www.zykhome.club/#/login "在线预览") & [后端项目](https://github.com/zykzhangyukang/Xinguan "后端项目") & [前端项目](https://github.com/zykzhangyukang/xinguan-vue "前端项目")
#### 模块划分 #### 模块划分

BIN
document/images/logo.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 32 KiB

View file

@ -1,17 +1,19 @@
package com.coderman.common.utils; package com.coderman.common.utils;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils;
import org.lionsoul.ip2region.DataBlock; import org.lionsoul.ip2region.DataBlock;
import org.lionsoul.ip2region.DbConfig; import org.lionsoul.ip2region.DbConfig;
import org.lionsoul.ip2region.DbSearcher; import org.lionsoul.ip2region.DbSearcher;
import org.lionsoul.ip2region.Util; import org.lionsoul.ip2region.Util;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;
import javax.servlet.http.HttpServletRequest;
import java.io.File; import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.Objects;
/** /**
* @author zhangyukang * @author zhangyukang
@ -20,47 +22,56 @@ public class AddressUtil {
private static Logger log = LoggerFactory.getLogger(AddressUtil.class); private static Logger log = LoggerFactory.getLogger(AddressUtil.class);
public static String getCityInfo(String ip){ @SuppressWarnings("all")
File file; public static String getCityInfo(String ip) {
try { //db
//db String dbPath = AddressUtil.class.getResource("/ip2region/ip2region.db").getPath();
String dbPath = AddressUtil.class.getResource("/ip2region/ip2region.db").getPath(); File file = new File(dbPath);
file= new File(dbPath);
if (!file.exists()) {
String tmpDir = System.getProperties().getProperty("java.io.tmpdir");
dbPath = tmpDir + "ip.db";
file = new File(dbPath);
FileUtils.copyInputStreamToFile(Objects.requireNonNull(AddressUtil.class.getClassLoader().getResourceAsStream("classpath:ip2region/ip2region.db")), file);
}
//查询算法 if (!file.exists()) {
int algorithm = DbSearcher.BTREE_ALGORITHM; //B-tree log.info("地址库文件不存在,进行其他处理");
//DbSearcher.BINARY_ALGORITHM //Binary String tmpDir = System.getProperties().getProperty("java.io.tmpdir");
//DbSearcher.MEMORY_ALGORITYM //Memory dbPath = tmpDir + File.separator + "ip2region.db";
try { log.info("临时文件路径:{}", dbPath);
DbConfig config = new DbConfig(); file = new File(dbPath);
DbSearcher searcher = new DbSearcher(config, dbPath); if (!file.exists() || (System.currentTimeMillis() - file.lastModified() > 86400000L)) {
log.info("文件不存在或者文件存在时间超过1天进入...");
//define the method try {
Method method = null; InputStream inputStream = new ClassPathResource("ip2region/ip2region.db").getInputStream();
method = searcher.getClass().getMethod("btreeSearch", String.class); IOUtils.copy(inputStream, new FileOutputStream(file));
} catch (IOException exception) {
DataBlock dataBlock = null; exception.printStackTrace();
if (!Util.isIpAddress(ip)) {
System.out.println("Error: Invalid ip address");
} }
dataBlock = (DataBlock) method.invoke(searcher, ip);
return dataBlock.getRegion();
} catch (Exception e) {
e.printStackTrace();
} }
}catch (Exception e) {
e.printStackTrace();
} }
//查询算法
int algorithm = DbSearcher.BTREE_ALGORITHM; //B-tree
try {
DbConfig config = new DbConfig();
DbSearcher searcher = new DbSearcher(config, dbPath);
//define the method
Method method = null;
switch (algorithm) {
case DbSearcher.BTREE_ALGORITHM:
method = searcher.getClass().getMethod("btreeSearch", String.class);
break;
case DbSearcher.BINARY_ALGORITHM:
method = searcher.getClass().getMethod("binarySearch", String.class);
break;
case DbSearcher.MEMORY_ALGORITYM:
method = searcher.getClass().getMethod("memorySearch", String.class);
break;
}
DataBlock dataBlock = null;
if (Util.isIpAddress(ip) == false) {
log.error("Error: Invalid ip address");
}
dataBlock = (DataBlock) method.invoke(searcher, ip);
return dataBlock.getRegion();
} catch (Exception e) {
e.printStackTrace();
}
return null; return null;
} }