|
@@ -1,6 +1,7 @@
|
|
|
package cc.mrbird.febs.server.system.utils;
|
|
package cc.mrbird.febs.server.system.utils;
|
|
|
|
|
|
|
|
import org.apache.commons.io.FileUtils;
|
|
import org.apache.commons.io.FileUtils;
|
|
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
import org.lionsoul.ip2region.DataBlock;
|
|
import org.lionsoul.ip2region.DataBlock;
|
|
|
import org.lionsoul.ip2region.DbConfig;
|
|
import org.lionsoul.ip2region.DbConfig;
|
|
|
import org.lionsoul.ip2region.DbSearcher;
|
|
import org.lionsoul.ip2region.DbSearcher;
|
|
@@ -10,8 +11,8 @@ import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
import java.io.File;
|
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
|
|
|
+import java.io.InputStream;
|
|
|
import java.lang.reflect.Method;
|
|
import java.lang.reflect.Method;
|
|
|
-import java.util.Objects;
|
|
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 根据 IP获取地址
|
|
* 根据 IP获取地址
|
|
@@ -31,7 +32,10 @@ public class AddressUtil {
|
|
|
String tmpDir = System.getProperties().getProperty("java.io.tmpdir");
|
|
String tmpDir = System.getProperties().getProperty("java.io.tmpdir");
|
|
|
dbPath = tmpDir + "ip.db";
|
|
dbPath = tmpDir + "ip.db";
|
|
|
file = new File(dbPath);
|
|
file = new File(dbPath);
|
|
|
- FileUtils.copyInputStreamToFile(Objects.requireNonNull(AddressUtil.class.getClassLoader().getResourceAsStream("classpath:ip2region/ip2region.db")), file);
|
|
|
|
|
|
|
+ InputStream resourceAsStream = AddressUtil.class.getClassLoader().getResourceAsStream("classpath:ip2region/ip2region.db");
|
|
|
|
|
+ if (resourceAsStream != null) {
|
|
|
|
|
+ FileUtils.copyInputStreamToFile(resourceAsStream, file);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
DbConfig config = new DbConfig();
|
|
DbConfig config = new DbConfig();
|
|
|
searcher = new DbSearcher(config, file.getPath());
|
|
searcher = new DbSearcher(config, file.getPath());
|
|
@@ -42,7 +46,8 @@ public class AddressUtil {
|
|
|
DataBlock dataBlock = (DataBlock) method.invoke(searcher, ip);
|
|
DataBlock dataBlock = (DataBlock) method.invoke(searcher, ip);
|
|
|
return dataBlock.getRegion();
|
|
return dataBlock.getRegion();
|
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
|
- log.error("获取地址信息异常", e);
|
|
|
|
|
|
|
+ log.error("获取地址信息异常,{}", e.getMessage());
|
|
|
|
|
+ return StringUtils.EMPTY;
|
|
|
} finally {
|
|
} finally {
|
|
|
if (searcher != null) {
|
|
if (searcher != null) {
|
|
|
try {
|
|
try {
|
|
@@ -52,7 +57,5 @@ public class AddressUtil {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
- return "";
|
|
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
}
|
|
}
|