|
@@ -14,9 +14,6 @@ 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;
|
|
@@ -44,59 +41,30 @@ public class IPUtils {
|
|
private static Searcher searcher;
|
|
private static Searcher searcher;
|
|
|
|
|
|
static {
|
|
static {
|
|
- initIpSearcher();
|
|
|
|
|
|
+ try {
|
|
|
|
+ initIpSearcher();
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ throw new RuntimeException(e);
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
* 初始化IP地址索引
|
|
* 初始化IP地址索引
|
|
*/
|
|
*/
|
|
- private static void initIpSearcher() {
|
|
|
|
- try {
|
|
|
|
- // 仅使用相对路径查找(工作目录、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) {}
|
|
|
|
-
|
|
|
|
- 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);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- log.error("ip2region初始化失败:未在相对路径找到ip2region.xdb,请将文件放在 ./ip/ip2region.xdb 或与jar同目录的ip目录中");
|
|
|
|
- searcher = null;
|
|
|
|
- } catch (Exception e) {
|
|
|
|
- log.error("ip2region初始化失败", e);
|
|
|
|
- searcher = null;
|
|
|
|
|
|
+ private static void initIpSearcher() throws IOException {
|
|
|
|
+ // use jar resource file file
|
|
|
|
+
|
|
|
|
+ try{
|
|
|
|
+ ClassLoader classLoader = IPUtils.class.getClassLoader();
|
|
|
|
+ if (classLoader.getResourceAsStream("ip/ip2region.xdb") == null) {
|
|
|
|
+ log.error("初始化IP地址索引失败");
|
|
|
|
+ }
|
|
|
|
+ log.info("初始化IP地址索引成功 (classpath)");
|
|
|
|
+ URL url = classLoader.getResource("ip/ip2region.xdb");
|
|
|
|
+ searcher = Searcher.newWithFileOnly(url.getPath());
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ log.error("初始化IP地址索引失败", e);
|
|
|
|
+ throw e;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|