|
@@ -8,7 +8,6 @@ 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;
|
|
@@ -17,6 +16,7 @@ import java.net.UnknownHostException;
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.nio.file.Files;
|
|
import java.nio.file.Files;
|
|
import java.nio.file.Path;
|
|
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;
|
|
@@ -51,7 +51,7 @@ public class IPUtils {
|
|
* 初始化IP地址索引
|
|
* 初始化IP地址索引
|
|
*/
|
|
*/
|
|
private static void initIpSearcher() {
|
|
private static void initIpSearcher() {
|
|
- /*try {
|
|
|
|
|
|
+ try {
|
|
// 仅使用相对路径查找(工作目录、jar同目录、常见容器目录)
|
|
// 仅使用相对路径查找(工作目录、jar同目录、常见容器目录)
|
|
java.util.List<Path> candidates = new java.util.ArrayList<>();
|
|
java.util.List<Path> candidates = new java.util.ArrayList<>();
|
|
try {
|
|
try {
|
|
@@ -97,45 +97,6 @@ public class IPUtils {
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
log.error("ip2region初始化失败", e);
|
|
log.error("ip2region初始化失败", e);
|
|
searcher = null;
|
|
searcher = null;
|
|
- }*/
|
|
|
|
- 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()) {
|
|
|
|
- searcher = Searcher.newWithFileOnly(externalPath.trim());
|
|
|
|
- log.info("IPLocationUtil ip2region初始化成功 (external path: {})", externalPath);
|
|
|
|
- return;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- // 2) 从classpath读取写入临时文件,再基于文件初始化
|
|
|
|
- try (InputStream is = IPUtils.class.getClassLoader().getResourceAsStream("ip/ip2region.xdb")) {
|
|
|
|
- if (is == null) {
|
|
|
|
- log.error("IPLocationUtil ip2region资源不存在: ip/ip2region.xdb");
|
|
|
|
- searcher = null;
|
|
|
|
- return;
|
|
|
|
- }
|
|
|
|
- byte[] cBuff = is.readAllBytes();
|
|
|
|
- Path tmp = Files.createTempFile("ip2region", ".xdb");
|
|
|
|
- Files.write(tmp, cBuff);
|
|
|
|
- subtleSetDeleteOnExit(tmp);
|
|
|
|
- searcher = Searcher.newWithFileOnly(tmp.toString());
|
|
|
|
- log.info("IPLocationUtil ip2region初始化成功 (temp file: {})", tmp);
|
|
|
|
- }
|
|
|
|
- } catch (Exception e) {
|
|
|
|
- log.error("IPLocationUtil ip2region初始化失败", e);
|
|
|
|
- searcher = null;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- private static void subtleSetDeleteOnExit(Path tmp) {
|
|
|
|
- try {
|
|
|
|
- java.io.File f = tmp.toFile();
|
|
|
|
- f.deleteOnExit();
|
|
|
|
- } catch (Exception ignore) {
|
|
|
|
- // no-op
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|