|
@@ -8,6 +8,7 @@ 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;
|
|
@@ -48,12 +49,15 @@ public class IPUtils {
|
|
* 初始化IP地址索引
|
|
* 初始化IP地址索引
|
|
*/
|
|
*/
|
|
private static void initIpSearcher() {
|
|
private static void initIpSearcher() {
|
|
- try {
|
|
|
|
- // 基于内存查询-从资源文件加载xdb数据库
|
|
|
|
- String dbPath = IPUtils.class.getClassLoader().getResource("ip/ip2region.xdb").getPath();
|
|
|
|
- byte[] cBuff = Searcher.loadContentFromFile(dbPath);
|
|
|
|
|
|
+ try (InputStream is = IPUtils.class.getClassLoader().getResourceAsStream("ip/ip2region.xdb")) {
|
|
|
|
+ if (is == null) {
|
|
|
|
+ log.error("ip2region资源不存在: ip/ip2region.xdb");
|
|
|
|
+ searcher = null;
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ byte[] cBuff = is.readAllBytes();
|
|
searcher = Searcher.newWithBuffer(cBuff);
|
|
searcher = Searcher.newWithBuffer(cBuff);
|
|
- log.info("ip2region初始化成功");
|
|
|
|
|
|
+ log.info("ip2region初始化成功 (classpath)");
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
log.error("ip2region初始化失败", e);
|
|
log.error("ip2region初始化失败", e);
|
|
searcher = null;
|
|
searcher = null;
|