|
@@ -8,13 +8,15 @@ import org.lionsoul.ip2region.xdb.Searcher;
|
|
|
|
|
|
import java.io.BufferedReader;
|
|
import java.io.BufferedReader;
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
-import java.io.InputStream;
|
|
|
|
import java.io.InputStreamReader;
|
|
import java.io.InputStreamReader;
|
|
import java.net.HttpURLConnection;
|
|
import java.net.HttpURLConnection;
|
|
import java.net.InetAddress;
|
|
import java.net.InetAddress;
|
|
import java.net.URL;
|
|
import java.net.URL;
|
|
import java.net.UnknownHostException;
|
|
import java.net.UnknownHostException;
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
+import java.nio.file.Files;
|
|
|
|
+import java.nio.file.Path;
|
|
|
|
+import java.nio.file.Paths;
|
|
import java.util.ArrayList;
|
|
import java.util.ArrayList;
|
|
import java.util.HashSet;
|
|
import java.util.HashSet;
|
|
import java.util.List;
|
|
import java.util.List;
|
|
@@ -50,54 +52,54 @@ public class IPUtils {
|
|
*/
|
|
*/
|
|
private static void initIpSearcher() {
|
|
private static void initIpSearcher() {
|
|
try {
|
|
try {
|
|
- // 1) 外部路径优先:系统属性 ip2region.db 或 环境变量 IP2REGION_DB_PATH
|
|
|
|
- String externalPath = System.getProperty("ip2region.db");
|
|
|
|
- if (externalPath == null || externalPath.trim().isEmpty()) {
|
|
|
|
- externalPath = System.getenv("IP2REGION_DB_PATH");
|
|
|
|
- }
|
|
|
|
- if (externalPath != null && !externalPath.trim().isEmpty()) {
|
|
|
|
- java.nio.file.Path p = java.nio.file.Paths.get(externalPath.trim());
|
|
|
|
- Long size = null;
|
|
|
|
- try { size = java.nio.file.Files.exists(p) ? java.nio.file.Files.size(p) : null; } catch (Exception ignore) {}
|
|
|
|
- log.info("ip2region准备使用外部文件: path={}, exists={}, size={}", externalPath, java.nio.file.Files.exists(p), size);
|
|
|
|
- searcher = Searcher.newWithFileOnly(externalPath.trim());
|
|
|
|
- log.info("ip2region初始化成功 (external path: {})", externalPath);
|
|
|
|
- return;
|
|
|
|
- }
|
|
|
|
|
|
+ // 仅使用相对路径查找(工作目录、jar同目录、常见容器目录)
|
|
|
|
+ java.util.List<Path> candidates = new java.util.ArrayList<>();
|
|
|
|
+ try {
|
|
|
|
+ Path cwd = Paths.get("").toAbsolutePath();
|
|
|
|
+ candidates.add(cwd.resolve("ip/ip2region.xdb"));
|
|
|
|
+ candidates.add(cwd.resolve("ip2region.xdb"));
|
|
|
|
+ // 以运行jar所在目录为基准
|
|
|
|
+ URL loc = IPUtils.class.getProtectionDomain().getCodeSource().getLocation();
|
|
|
|
+ if (loc != null) {
|
|
|
|
+ Path base;
|
|
|
|
+ try {
|
|
|
|
+ Path jarPath = Paths.get(loc.toURI());
|
|
|
|
+ base = Files.isRegularFile(jarPath) ? jarPath.getParent() : jarPath;
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ base = null;
|
|
|
|
+ }
|
|
|
|
+ if (base != null) {
|
|
|
|
+ candidates.add(base.resolve("ip/ip2region.xdb"));
|
|
|
|
+ candidates.add(base.resolve("../ip/ip2region.xdb"));
|
|
|
|
+ candidates.add(base.resolve("ip2region.xdb"));
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ // 常见容器工作目录
|
|
|
|
+ candidates.add(Paths.get("/pig-marketing/ip/ip2region.xdb"));
|
|
|
|
+ } catch (Exception ignore) {}
|
|
|
|
|
|
- // 2) 从classpath读取写入临时文件,再基于文件初始化
|
|
|
|
- try (InputStream is = IPUtils.class.getClassLoader().getResourceAsStream("ip/ip2region.xdb")) {
|
|
|
|
- if (is == null) {
|
|
|
|
- log.error("ip2region资源不存在: ip/ip2region.xdb");
|
|
|
|
- searcher = null;
|
|
|
|
- return;
|
|
|
|
|
|
+ for (Path cand : candidates) {
|
|
|
|
+ try {
|
|
|
|
+ if (cand != null && Files.exists(cand)) {
|
|
|
|
+ Long sz = null; try { sz = Files.size(cand); } catch (Exception ignore) {}
|
|
|
|
+ log.info("ip2region尝试相对路径: path={}, size={}", cand, sz);
|
|
|
|
+ searcher = Searcher.newWithFileOnly(cand.toString());
|
|
|
|
+ log.info("ip2region初始化成功 (relative path: {})", cand);
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ } catch (Exception tryNext) {
|
|
|
|
+ log.warn("ip2region相对路径初始化失败: {}", cand, tryNext);
|
|
}
|
|
}
|
|
- byte[] cBuff = is.readAllBytes();
|
|
|
|
- java.net.URL resUrl = IPUtils.class.getClassLoader().getResource("ip/ip2region.xdb");
|
|
|
|
- log.info("ip2region从classpath读取: url={}, bytes={}", resUrl, cBuff != null ? cBuff.length : null);
|
|
|
|
- java.nio.file.Path tmp = java.nio.file.Files.createTempFile("ip2region", ".xdb");
|
|
|
|
- java.nio.file.Files.write(tmp, cBuff);
|
|
|
|
- subtleSetDeleteOnExit(tmp);
|
|
|
|
- Long tmpSize = null; try { tmpSize = java.nio.file.Files.size(tmp); } catch (Exception ignore) {}
|
|
|
|
- log.info("ip2region写入临时文件: path={}, size={} (bytes)", tmp, tmpSize);
|
|
|
|
- searcher = Searcher.newWithFileOnly(tmp.toString());
|
|
|
|
- log.info("ip2region初始化成功 (temp file: {})", tmp);
|
|
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ log.error("ip2region初始化失败:未在相对路径找到ip2region.xdb,请将文件放在 ./ip/ip2region.xdb 或与jar同目录的ip目录中");
|
|
|
|
+ searcher = null;
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
log.error("ip2region初始化失败", e);
|
|
log.error("ip2region初始化失败", e);
|
|
searcher = null;
|
|
searcher = null;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- private static void subtleSetDeleteOnExit(java.nio.file.Path tmp) {
|
|
|
|
- try {
|
|
|
|
- java.io.File f = tmp.toFile();
|
|
|
|
- f.deleteOnExit();
|
|
|
|
- } catch (Exception ignore) {
|
|
|
|
- // no-op
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
/**
|
|
/**
|
|
* 获取IP地址
|
|
* 获取IP地址
|
|
*
|
|
*
|