Explorar o código

保存记录接口

zhoutg %!s(int64=5) %!d(string=hai) anos
pai
achega
a78e66393f

+ 0 - 1
diagbotman-service/src/main/java/com/diagbot/config/ResourceServerConfigurer.java

@@ -32,7 +32,6 @@ public class ResourceServerConfigurer extends ResourceServerConfigurerAdapter {
                 .antMatchers("/productOrder/getUserWaitingRenewal").permitAll()
                 .antMatchers("/productOrder/getOrderByUserToAudit").permitAll()
                 .antMatchers("/optInfo/save").permitAll()
-                .antMatchers("/optInfo/save1").permitAll()
                 .antMatchers("/**").authenticated();
         //        .antMatchers("/**").permitAll();
     }

+ 0 - 1
diagbotman-service/src/main/java/com/diagbot/config/security/UrlAccessDecisionManager.java

@@ -96,7 +96,6 @@ public class UrlAccessDecisionManager implements AccessDecisionManager {
                 || matchers("/productOrder/getUserWaitingRenewal", request)
                 || matchers("/productOrder/getOrderByUserToAudit", request)
                 || matchers("/optInfo/save", request)
-                || matchers("/optInfo/save1", request)
                 || matchers("/", request)) {
             return true;
         }

+ 37 - 98
diagbotman-service/src/main/java/com/diagbot/util/IPUtil.java

@@ -6,16 +6,11 @@ import org.lionsoul.ip2region.DbConfig;
 import org.lionsoul.ip2region.DbSearcher;
 import org.lionsoul.ip2region.Util;
 import org.springframework.core.io.ClassPathResource;
-import org.springframework.core.io.DefaultResourceLoader;
 import org.springframework.core.io.Resource;
-import org.springframework.core.io.ResourceLoader;
 import org.springframework.util.FileCopyUtils;
 
 import java.io.File;
-import java.io.FileOutputStream;
-import java.io.IOException;
 import java.io.InputStream;
-import java.io.OutputStream;
 import java.lang.reflect.Method;
 
 /**
@@ -25,20 +20,44 @@ import java.lang.reflect.Method;
  */
 public class IPUtil {
 
-    //国家,区域,省份,城市,运营商
-    public static String getCityInfo(String ip) {
+    private static DbSearcher searcher;
 
+    /**
+     *  获取DbSearcher
+     * @return
+     */
+    public static DbSearcher getInstance () {
+        if (searcher != null) {
+            return searcher;
+        }
+        try {
+            DbConfig config = new DbConfig();
+            File file = new File("/ip2region.db"); // 本路获取资源文件
+            if (!file.exists()) {
+                Resource resource = new ClassPathResource("/ip2region.db"); // 远程获取资源文件
+                InputStream inputStream = resource.getInputStream();
+                searcher = new DbSearcher(config, FileCopyUtils.copyToByteArray(inputStream));
+            } else {
+                searcher = new DbSearcher(config, file.getPath());
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+            System.out.println("获取DbSearcher错误");
+        }
+        return searcher;
+    }
+
+
+    //国家,区域,省份,城市,运营商
+    public  static String getCityInfo(String ip) {
         //查询算法
-        int algorithm = DbSearcher.BTREE_ALGORITHM; //B-tree
+        int algorithm = DbSearcher.MEMORY_ALGORITYM;
+         //DbSearcher.BTREE_ALGORITHM; //B-tree
         //DbSearcher.BINARY_ALGORITHM //Binary
         //DbSearcher.MEMORY_ALGORITYM //Memory
         try {
-            //db
-            ResourceLoader resourceLoader = new DefaultResourceLoader();
-            InputStream stream = resourceLoader.getResource("classpath:ip2region.db").getInputStream();
-
-            DbConfig config = new DbConfig();
-            DbSearcher searcher = new DbSearcher(config, FileCopyUtils.copyToByteArray(stream));
+            // 初始化
+            getInstance();
 
             //define the method
             Method method = null;
@@ -65,9 +84,9 @@ public class IPUtil {
 
         } catch (Exception e) {
             e.printStackTrace();
+            System.out.println("Ip解析错误");
         }
-
-        return "0|0|0|内网IP|内网IP";
+        return null;
     }
 
 
@@ -86,88 +105,8 @@ public class IPUtil {
         return new IpEntity();
     }
 
-    public static void inputstreamtofile(InputStream ins, File file) {
-        try {
-            OutputStream os = new FileOutputStream(file);
-            int bytesRead = 0;
-            byte[] buffer = new byte[8192];
-            while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
-                os.write(buffer, 0, bytesRead);
-            }
-            os.close();
-            ins.close();
-        } catch (Exception e) {
-            e.printStackTrace();
-        }
-    }
-
-
-    //国家,区域,省份,城市,运营商
-    public  static String getCityInfo1(String ip) {
-
-        //db
-        String dbPath = "";
-        try {
-//            File file = ResourceUtils.getFile("classpath:ip2region.db");
-
-            File file = new File("/ip2region.db");//临时图片存在的位置
-            if (!file.exists()) {
-                Resource resource = new ClassPathResource("/ip2region.db");
-                try {
-                    InputStream inputStream = resource.getInputStream();
-                    inputstreamtofile(inputStream, file);
-                } catch (IOException e) {
-                    e.printStackTrace();
-                }
-//                InputStream in = this.getClass().getResourceAsStream("/ip2region.db");//图片在项目中的位置
-//                FileUtil.inputstreamtofile(in, f);
-            }
-
-            dbPath = file.getPath();
-        } catch (Exception 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;
-    }
-
     public static void main(String[] args) {
-        System.out.println(getCityInfo1("192.168.3.117"));
+        System.out.println(getCityInfo("223.93.170.82"));
+        System.out.println(getCityInfo("223.93.170.82"));
     }
 }

+ 0 - 10
diagbotman-service/src/main/java/com/diagbot/web/OptInfoController.java

@@ -4,7 +4,6 @@ package com.diagbot.web;
 import com.diagbot.annotation.SysLogger;
 import com.diagbot.dto.RespDTO;
 import com.diagbot.facade.OptInfoFacade;
-import com.diagbot.util.IPUtil;
 import com.diagbot.vo.OptInfoVO;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
@@ -40,13 +39,4 @@ public class OptInfoController {
         return RespDTO.onSuc(optInfoFacade.saveFac(optInfoVO));
     }
 
-    @ApiOperation(value = "保存记录[by:zhoutg]",
-            notes = "productId:产品id<br>" +
-                    "username:用户名<br>" +
-                    "linkman:联系人<br>")
-    @PostMapping("/save1")
-    @SysLogger("save1")
-    public RespDTO<Boolean> save1() {
-        return RespDTO.onSuc(IPUtil.getCityInfo1("192.168.3.117"));
-    }
 }