wangliang 2 years ago
parent
commit
394370fd63
1 changed files with 19 additions and 56 deletions
  1. 19 56
      themis-common/src/main/java/com/qmth/themis/common/util/IpUtil.java

+ 19 - 56
themis-common/src/main/java/com/qmth/themis/common/util/IpUtil.java

@@ -14,8 +14,16 @@ import java.io.InputStream;
 import java.util.Objects;
 import java.util.Optional;
 
+/**
+ * @Description: ip util
+ * @Param:
+ * @return:
+ * @Author: wangliang
+ * @Date: 2022/9/2
+ */
 public class IpUtil {
     private final static Logger log = LoggerFactory.getLogger(IpUtil.class);
+    public static final String IP_REGION_FILE = "ip2region.xdb";
 
     /**
      * 获取过程ip(默认不包含代理ip)
@@ -66,58 +74,6 @@ public class IpUtil {
         return ip;
     }
 
-//    /**
-//     * 根据IP地址获取城市
-//     *
-//     * @param ip 如果放在服务器读取不了,两种方式
-//     *           1.可以在服务器上创建文件,ip2region.db这个放在文件里面,然后开始读取
-//     *           2.可以整个配置文件,在配置配置目录,然后读取配置文件
-//     * @return
-//     */
-//    public static String getCityInfo(String ip) throws IOException {
-//        URL url = IpUtil.class.getClassLoader().getResource("ip2region.db");
-//        File file = Objects.nonNull(url) ? new File(url.getFile()) : null;
-//        if (Objects.isNull(file)) {
-//            return null;
-//        }
-//        if (!file.exists()) {
-//            log.error("Error: Invalid ip2region.db file, filePath:" + file.getPath());
-//            return null;
-//        }
-//        //查询算法
-//        int algorithm = DbSearcher.BTREE_ALGORITHM; //B-tree
-//        //DbSearcher.BINARY_ALGORITHM //Binary
-//        //DbSearcher.MEMORY_ALGORITYM //Memory
-//        try {
-//            DbConfig config = new DbConfig();
-//            DbSearcher searcher = new DbSearcher(config, file.getPath());
-//            Method method;
-//            switch (algorithm) {
-//                case DbSearcher.BTREE_ALGORITHM:
-//                    method = searcher.getClass().getMethod("btreeSearch", String.class);
-//                    break;
-//                case DbSearcher.BINARY_ALGORITHM:
-//                    method = searcher.getClass().getMethod("binarySearch", String.class);
-//                    break;
-//                case DbSearcher.MEMORY_ALGORITYM:
-//                    method = searcher.getClass().getMethod("memorySearch", String.class);
-//                    break;
-//                default:
-//                    return null;
-//            }
-//            DataBlock dataBlock;
-//            if (!Util.isIpAddress(ip)) {
-//                log.error("Error: Invalid ip address");
-//                return null;
-//            }
-//            dataBlock = (DataBlock) method.invoke(searcher, ip);
-//            return dataBlock.getRegion();
-//        } catch (Exception e) {
-//            e.printStackTrace();
-//        }
-//        return null;
-//    }
-
     /**
      * 获取全局searcher
      *
@@ -125,14 +81,14 @@ public class IpUtil {
      * @return
      */
     public static Searcher getSearcher(String path) throws IOException {
-        InputStream inputStream = IpUtil.class.getClassLoader().getResourceAsStream("ip2region.xdb");
-        Optional.ofNullable(inputStream).orElseThrow(() -> new BusinessException("ip2region.xdb未找到"));
-        File file = new File(path + File.separator + "ip2region.xdb");
+        InputStream inputStream = IpUtil.class.getClassLoader().getResourceAsStream(IP_REGION_FILE);
+        Optional.ofNullable(inputStream).orElseThrow(() -> new BusinessException(IP_REGION_FILE + "未找到"));
+        File file = new File(path + File.separator + IP_REGION_FILE);
         if (!file.exists()) {
             file.getParentFile().mkdirs();
             file.createNewFile();
-            FileUtils.copyInputStreamToFile(inputStream, file);
         }
+        FileUtils.copyInputStreamToFile(inputStream, file);
         // 1、从 dbPath 加载整个 xdb 到内存。
         byte[] cBuff;
         try {
@@ -144,6 +100,13 @@ public class IpUtil {
             } else {
                 throw new RuntimeException(e);
             }
+        } finally {
+            if (Objects.nonNull(inputStream)) {
+                inputStream.close();
+            }
+            if (Objects.nonNull(file)) {
+                file.delete();
+            }
         }
 
         // 2、使用上述的 cBuff 创建一个完全基于内存的查询对象。