Jelajahi Sumber

保存记录接口

zhoutg 5 tahun lalu
induk
melakukan
5bc8f167c4

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

@@ -32,6 +32,7 @@ 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();
     }

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

@@ -96,6 +96,7 @@ 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;
         }

+ 91 - 0
diagbotman-service/src/main/java/com/diagbot/util/IPUtil.java

@@ -5,11 +5,17 @@ import org.lionsoul.ip2region.DataBlock;
 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;
 
 /**
@@ -79,4 +85,89 @@ 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"));
+    }
 }

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

@@ -4,6 +4,7 @@ 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;
@@ -38,4 +39,14 @@ public class OptInfoController {
     public RespDTO<Boolean> save(@RequestBody OptInfoVO optInfoVO) {
         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"));
+    }
 }