Преглед на файлове

Merge branch 'dev/wcl' of https://s-20coaj910c.zht2.com/lwh/seo into dev/lwh

lwh преди 1 седмица
родител
ревизия
54b7054c65
променени са 1 файла, в които са добавени 16 реда и са изтрити 12 реда
  1. 16 12
      pig-marketing/pig-marketing-biz/src/main/java/com/pig4cloud/pig/marketing/util/IPLocationUtil.java

+ 16 - 12
pig-marketing/pig-marketing-biz/src/main/java/com/pig4cloud/pig/marketing/util/IPLocationUtil.java

@@ -8,6 +8,7 @@ import org.lionsoul.ip2region.xdb.Searcher;
 import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.stereotype.Component;
 
+import java.io.InputStream;
 import java.util.List;
 import java.util.concurrent.TimeUnit;
 
@@ -36,18 +37,21 @@ public class IPLocationUtil {
     /**
      * 初始化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地址获取三级地址信息