|
@@ -12,6 +12,7 @@ import java.io.File;
|
|
|
import java.io.IOException;
|
|
|
import java.io.InputStream;
|
|
|
import java.util.Objects;
|
|
|
+import java.util.Optional;
|
|
|
|
|
|
public class IpUtil {
|
|
|
private final static Logger log = LoggerFactory.getLogger(IpUtil.class);
|
|
@@ -123,25 +124,14 @@ public class IpUtil {
|
|
|
* @param path
|
|
|
* @return
|
|
|
*/
|
|
|
- public static Searcher getSearcher(String path) {
|
|
|
- File file = null;
|
|
|
+ public static Searcher getSearcher(String path) throws IOException {
|
|
|
InputStream inputStream = IpUtil.class.getClassLoader().getResourceAsStream("ip2region.xdb");
|
|
|
- if (Objects.isNull(inputStream)) {
|
|
|
- log.info("inputstream is null");
|
|
|
- } else {
|
|
|
- file = new File(path + File.separator + "ip2region.xdb");
|
|
|
- try {
|
|
|
- FileUtils.copyInputStreamToFile(inputStream, file);
|
|
|
- } catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- }
|
|
|
- if (Objects.isNull(file)) {
|
|
|
- return null;
|
|
|
- }
|
|
|
+ Optional.ofNullable(inputStream).orElseThrow(() -> new BusinessException("ip2region.xdb未找到"));
|
|
|
+ File file = new File(path + File.separator + "ip2region.xdb");
|
|
|
if (!file.exists()) {
|
|
|
- log.error("Error: Invalid ip2region.xdb file, filePath:{}", file.getPath());
|
|
|
- return null;
|
|
|
+ file.getParentFile().mkdirs();
|
|
|
+ file.createNewFile();
|
|
|
+ FileUtils.copyInputStreamToFile(inputStream, file);
|
|
|
}
|
|
|
// 1、从 dbPath 加载整个 xdb 到内存。
|
|
|
byte[] cBuff;
|