update 优化 直接从ClassPath加载ip2region数据库文件
This commit is contained in:
parent
6d44069364
commit
5e51077347
@ -1,15 +1,12 @@
|
|||||||
package org.dromara.common.core.utils.ip;
|
package org.dromara.common.core.utils.ip;
|
||||||
|
|
||||||
import cn.hutool.core.io.FileUtil;
|
import cn.hutool.core.io.resource.NoResourceException;
|
||||||
import cn.hutool.core.io.resource.ClassPathResource;
|
import cn.hutool.core.io.resource.ResourceUtil;
|
||||||
import cn.hutool.core.util.ObjectUtil;
|
|
||||||
import org.dromara.common.core.exception.ServiceException;
|
|
||||||
import org.dromara.common.core.utils.file.FileUtils;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.dromara.common.core.exception.ServiceException;
|
||||||
|
import org.dromara.common.core.utils.StringUtils;
|
||||||
import org.lionsoul.ip2region.xdb.Searcher;
|
import org.lionsoul.ip2region.xdb.Searcher;
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据ip地址定位工具类,离线方式
|
* 根据ip地址定位工具类,离线方式
|
||||||
* 参考地址:<a href="https://gitee.com/lionsoul/ip2region/tree/master/binding/java">集成 ip2region 实现离线IP地址定位库</a>
|
* 参考地址:<a href="https://gitee.com/lionsoul/ip2region/tree/master/binding/java">集成 ip2region 实现离线IP地址定位库</a>
|
||||||
@ -19,31 +16,19 @@ import java.io.File;
|
|||||||
@Slf4j
|
@Slf4j
|
||||||
public class RegionUtils {
|
public class RegionUtils {
|
||||||
|
|
||||||
|
// IP地址库文件名称
|
||||||
|
public static final String IP_XDB_FILENAME = "ip2region.xdb";
|
||||||
|
|
||||||
private static final Searcher SEARCHER;
|
private static final Searcher SEARCHER;
|
||||||
|
|
||||||
static {
|
static {
|
||||||
String fileName = "/ip2region.xdb";
|
|
||||||
File existFile = FileUtils.file(FileUtil.getTmpDir() + FileUtil.FILE_SEPARATOR + fileName);
|
|
||||||
if (!FileUtils.exist(existFile)) {
|
|
||||||
ClassPathResource fileStream = new ClassPathResource(fileName);
|
|
||||||
if (ObjectUtil.isEmpty(fileStream.getStream())) {
|
|
||||||
throw new ServiceException("RegionUtils初始化失败,原因:IP地址库数据不存在!");
|
|
||||||
}
|
|
||||||
FileUtils.writeFromStream(fileStream.getStream(), existFile);
|
|
||||||
}
|
|
||||||
|
|
||||||
String dbPath = existFile.getPath();
|
|
||||||
|
|
||||||
// 1、从 dbPath 加载整个 xdb 到内存。
|
|
||||||
byte[] cBuff;
|
|
||||||
try {
|
try {
|
||||||
cBuff = Searcher.loadContentFromFile(dbPath);
|
// 1、将 ip2region 数据库文件 xdb 从 ClassPath 加载到内存。
|
||||||
} catch (Exception e) {
|
// 2、基于加载到内存的 xdb 数据创建一个 Searcher 查询对象。
|
||||||
throw new ServiceException("RegionUtils初始化失败,原因:从ip2region.xdb文件加载内容失败!" + e.getMessage());
|
SEARCHER = Searcher.newWithBuffer(ResourceUtil.readBytes(IP_XDB_FILENAME));
|
||||||
}
|
log.info("RegionUtils初始化成功,加载IP地址库数据成功!");
|
||||||
// 2、使用上述的 cBuff 创建一个完全基于内存的查询对象。
|
} catch (NoResourceException e) {
|
||||||
try {
|
throw new ServiceException("RegionUtils初始化失败,原因:IP地址库数据不存在!");
|
||||||
SEARCHER = Searcher.newWithBuffer(cBuff);
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new ServiceException("RegionUtils初始化失败,原因:" + e.getMessage());
|
throw new ServiceException("RegionUtils初始化失败,原因:" + e.getMessage());
|
||||||
}
|
}
|
||||||
@ -54,9 +39,8 @@ public class RegionUtils {
|
|||||||
*/
|
*/
|
||||||
public static String getCityInfo(String ip) {
|
public static String getCityInfo(String ip) {
|
||||||
try {
|
try {
|
||||||
ip = ip.trim();
|
|
||||||
// 3、执行查询
|
// 3、执行查询
|
||||||
String region = SEARCHER.search(ip);
|
String region = SEARCHER.search(StringUtils.trim(ip));
|
||||||
return region.replace("0|", "").replace("|0", "");
|
return region.replace("0|", "").replace("|0", "");
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("IP地址离线获取城市异常 {}", ip);
|
log.error("IP地址离线获取城市异常 {}", ip);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user