|
@@ -8,6 +8,7 @@ import org.lionsoul.ip2region.xdb.Searcher;
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
|
|
+import java.io.InputStream;
|
|
import java.util.List;
|
|
import java.util.List;
|
|
import java.util.concurrent.TimeUnit;
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
@@ -36,18 +37,21 @@ public class IPLocationUtil {
|
|
/**
|
|
/**
|
|
* 初始化IP地址索引
|
|
* 初始化IP地址索引
|
|
*/
|
|
*/
|
|
- private static void initIpSearcher() {
|
|
|
|
- try {
|
|
|
|
- // 基于内存查询-从资源文件加载xdb数据库
|
|
|
|
- String dbPath = IPLocationUtil.class.getClassLoader().getResource("ip/ip2region.xdb").getPath();
|
|
|
|
- byte[] cBuff = Searcher.loadContentFromFile(dbPath);
|
|
|
|
- searcher = Searcher.newWithBuffer(cBuff);
|
|
|
|
- log.info("IPLocationUtil ip2region初始化成功");
|
|
|
|
- } catch (Exception e) {
|
|
|
|
- log.error("IPLocationUtil ip2region初始化失败", e);
|
|
|
|
- searcher = null;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
|
|
+ private static void initIpSearcher() {
|
|
|
|
+ try (InputStream is = IPLocationUtil.class.getClassLoader().getResourceAsStream("ip/ip2region.xdb")) {
|
|
|
|
+ if (is == null) {
|
|
|
|
+ log.error("IPLocationUtil ip2region资源不存在: ip/ip2region.xdb");
|
|
|
|
+ searcher = null;
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ byte[] cBuff = is.readAllBytes();
|
|
|
|
+ searcher = Searcher.newWithBuffer(cBuff);
|
|
|
|
+ log.info("IPLocationUtil ip2region初始化成功 (classpath)");
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ log.error("IPLocationUtil ip2region初始化失败", e);
|
|
|
|
+ searcher = null;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
/**
|
|
* 根据IP地址获取三级地址信息
|
|
* 根据IP地址获取三级地址信息
|