浏览代码

Resource文件读取

gaodm 5 年之前
父节点
当前提交
adbfadf120

+ 98 - 0
diagbotman-service/src/main/java/com/diagbot/util/IPLocalUtil.java

@@ -0,0 +1,98 @@
+package com.diagbot.util;
+
+import com.diagbot.entity.IpEntity;
+import org.lionsoul.ip2region.DataBlock;
+import org.lionsoul.ip2region.DbConfig;
+import org.lionsoul.ip2region.DbSearcher;
+import org.lionsoul.ip2region.Util;
+import org.springframework.util.ResourceUtils;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.lang.reflect.Method;
+
+/**
+ * @Description:
+ * @author: gaodm
+ * @time: 2020/3/24 18:29
+ */
+public class IPLocalUtil {
+
+    //国家,区域,省份,城市,运营商
+    public static String getCityInfo(String ip) {
+
+        //db
+        String dbPath = "";
+        try {
+            File file = ResourceUtils.getFile("classpath:ip2region.db");
+            if (file.exists() == false) {
+                System.out.println("Error: Invalid ip2region.db file");
+            }
+            dbPath = file.getPath();
+        } catch (FileNotFoundException e) {
+            System.out.println("文件读取错误");
+        }
+
+        //查询算法
+        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, dbPath);
+
+            //define the method
+            Method method = null;
+            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;
+            }
+
+            DataBlock dataBlock = null;
+            if (Util.isIpAddress(ip) == false) {
+                System.out.println("Error: Invalid ip address");
+            }
+
+            dataBlock = (DataBlock) method.invoke(searcher, ip);
+
+            return dataBlock.getRegion();
+
+        } catch (Exception e) {
+            e.printStackTrace();
+            System.out.println("Ip解析错误");
+        }
+
+        return null;
+    }
+
+
+    /**
+     * 返回ip实体类
+     *
+     * @param ip
+     * @return ip实体类
+     */
+    public static IpEntity getCityInfoWithEntity(String ip) {
+        String ipStr = getCityInfo(ip);
+        String[] arr = ipStr.split("\\|");
+        if (arr != null && arr.length == 5) {
+            return new IpEntity(arr[0], arr[1], arr[2], arr[3], arr[4]);
+        }
+        return new IpEntity();
+    }
+
+    public static void main(String[] args) throws Exception {
+        System.err.println(getCityInfo("220.248.12.158"));
+        System.err.println(getCityInfo("223.93.170.82"));
+        System.err.println(getCityInfo("192.168.0.1"));
+        System.err.println(getCityInfo("220.228.12.159"));
+        System.err.println(getCityInfo("210.228.12.159"));
+    }
+}

+ 10 - 26
diagbotman-service/src/main/java/com/diagbot/util/IPUtil.java

@@ -5,10 +5,11 @@ 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;
 import org.lionsoul.ip2region.Util;
 import org.lionsoul.ip2region.Util;
-import org.springframework.util.ResourceUtils;
+import org.springframework.core.io.DefaultResourceLoader;
+import org.springframework.core.io.ResourceLoader;
+import org.springframework.util.FileCopyUtils;
 
 
-import java.io.File;
-import java.io.FileNotFoundException;
+import java.io.InputStream;
 import java.lang.reflect.Method;
 import java.lang.reflect.Method;
 
 
 /**
 /**
@@ -21,25 +22,17 @@ public class IPUtil {
     //国家,区域,省份,城市,运营商
     //国家,区域,省份,城市,运营商
     public static String getCityInfo(String ip) {
     public static String getCityInfo(String ip) {
 
 
-        //db
-        String dbPath = "";
-        try {
-            File file = ResourceUtils.getFile("classpath:ip2region.db");
-            if (file.exists() == false) {
-                System.out.println("Error: Invalid ip2region.db file");
-            }
-            dbPath = file.getPath();
-        } catch (FileNotFoundException e) {
-            System.out.println("文件读取错误");
-        }
-
         //查询算法
         //查询算法
         int algorithm = DbSearcher.BTREE_ALGORITHM; //B-tree
         int algorithm = DbSearcher.BTREE_ALGORITHM; //B-tree
         //DbSearcher.BINARY_ALGORITHM //Binary
         //DbSearcher.BINARY_ALGORITHM //Binary
         //DbSearcher.MEMORY_ALGORITYM //Memory
         //DbSearcher.MEMORY_ALGORITYM //Memory
         try {
         try {
+            //db
+            ResourceLoader resourceLoader = new DefaultResourceLoader();
+            InputStream stream = resourceLoader.getResource("classpath:ip2region.db").getInputStream();
+
             DbConfig config = new DbConfig();
             DbConfig config = new DbConfig();
-            DbSearcher searcher = new DbSearcher(config, dbPath);
+            DbSearcher searcher = new DbSearcher(config, FileCopyUtils.copyToByteArray(stream));
 
 
             //define the method
             //define the method
             Method method = null;
             Method method = null;
@@ -66,10 +59,9 @@ public class IPUtil {
 
 
         } catch (Exception e) {
         } catch (Exception e) {
             e.printStackTrace();
             e.printStackTrace();
-            System.out.println("Ip解析错误");
         }
         }
 
 
-        return null;
+        return "0|0|0|内网IP|内网IP";
     }
     }
 
 
 
 
@@ -87,12 +79,4 @@ public class IPUtil {
         }
         }
         return new IpEntity();
         return new IpEntity();
     }
     }
-
-    public static void main(String[] args) throws Exception {
-        System.err.println(getCityInfo("220.248.12.158"));
-        System.err.println(getCityInfo("223.93.170.82"));
-        System.err.println(getCityInfo("192.168.0.1"));
-        System.err.println(getCityInfo("220.228.12.159"));
-        System.err.println(getCityInfo("210.228.12.159"));
-    }
 }
 }