Browse Source

1. 批量下载二维码
2. 修改配置文件

wuweihao 4 năm trước cách đây
mục cha
commit
b1cd6dda90

+ 5 - 2
gis_application/src/main/resources/application-dev.properties

@@ -4,8 +4,11 @@
 spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
 spring.datasource.druid.url=jdbc:mysql://localhost:3306/${project.name}?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&characterSetResults=utf8
-spring.datasource.druid.username=root
 spring.datasource.druid.password=root
+spring.datasource.druid.username=root
+# 生产数据
+#spring.datasource.druid.url=jdbc:mysql://120.24.33.137:3306/${project.name}?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&characterSetResults=utf8
+#spring.datasource.druid.password=123456
 
 
 # \u521D\u59CB\u8FDE\u63A5\u6570
@@ -56,7 +59,7 @@ logging.level.com.gis=debug
 
 # 上传文件保存路径
 # 本地保存路径
-file.path=F:\\test\\ngin\\${project.name}_data\\
+server.file.path=F:\\test\\ngin\\${project.name}_data\\
 
 # oss info
 oss.file.path=${project.name}/

+ 1 - 1
gis_application/src/main/resources/application-pro.properties

@@ -53,7 +53,7 @@ logging.config=classpath:logback-spring.xml
 logging.level.com.gis=debug
 
 # \u672C\u5730\u4FDD\u5B58\u8DEF\u5F84
-file.path=/root/user/${project.name}_data/
+server.file.path=/root/user/${project.name}_data/
 
 
 # oss info

+ 1 - 1
gis_application/src/main/resources/application-sit.properties

@@ -53,7 +53,7 @@ logging.config=classpath:logback-spring.xml
 logging.level.com.gis=debug
 
 # \u672C\u5730\u4FDD\u5B58\u8DEF\u5F84
-file.path=/root/user/${project.name}_data/
+server.file.path=/root/user/${project.name}_data/
 
 
 # oss info

+ 68 - 0
gis_common/src/main/java/com/gis/common/constant/ConfigConstant.java

@@ -0,0 +1,68 @@
+package com.gis.common.constant;
+
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Component;
+
+/**
+ * Created by owen on 2020/12/31 0031 14:22
+ *
+ * 全局动态参数
+ */
+@Component
+public class ConfigConstant {
+
+    /** 服务器文件地址*/
+    @Value("${server.file.path}")
+    public  String serverBasePath;
+
+    /**redis token前缀*/
+//    @Value("${redis.prefix}")
+//    public  String redisPrefix;
+
+//    /**允许上传的文件后缀*/
+//    @Value("${file.allow}")
+//    public String fileAllow;
+
+//    @Value("${project.en}")
+//    public String projectEn;
+
+//    @Value("${project.name}")
+//    public String serverDomain;
+
+//    @Value("${oss.point}")
+//    public  String ossPoint;
+//
+//    @Value("${oss.key}")
+//    public  String ossKey;
+//
+//    @Value("${oss.secrecy}")
+//    public  String ossSecrecy;
+//
+//    @Value("${oss.bucket}")
+//    public  String ossBucket;
+//
+    @Value("${oss.file.path}")
+    public  String ossBasePath;
+//
+    @Value("${oss.domain}")
+    public  String ossDomain;
+//
+//
+//    @Value("${swagger.package}")
+//    public  String swaggerPackage;
+//
+//    @Value("${swagger.title}")
+//    public  String swaggerTitle;
+//
+//    @Value("${swagger.description}")
+//    public  String swaggerDescription;
+//
+//    @Value("${swagger.version}")
+//    public  String swaggerVersion;
+
+
+
+
+
+
+}

+ 88 - 3
gis_common/src/main/java/com/gis/common/util/FileUtils.java

@@ -9,9 +9,8 @@ import org.junit.Test;
 import org.springframework.util.ResourceUtils;
 import org.springframework.web.multipart.MultipartFile;
 
-import java.io.File;
-import java.io.IOException;
-import java.io.InputStream;
+import java.io.*;
+import java.net.HttpURLConnection;
 import java.net.URL;
 import java.util.*;
 //import com.gis.common.util.RandomUtils;
@@ -353,4 +352,90 @@ public class FileUtils {
         // 默认返回类型
         return "image";
     }
+
+    /**
+     * 从网络Url中下载文件
+     *
+     * @param urlStr
+     * @param fileName 需要带后缀
+     * @param savePath
+     * @return
+     * @throws IOException
+     */
+    public static boolean downLoadFromUrl(String urlStr, String fileName, String savePath){
+        FileOutputStream fos = null;
+        InputStream inputStream = null;
+        try {
+            URL url = new URL(urlStr);
+            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
+            // 设置超时间为3秒
+            conn.setConnectTimeout(3 * 1000);
+            // 防止屏蔽程序抓取而返回403错误
+            conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
+
+            // 得到输入流
+            inputStream = conn.getInputStream();
+            // 获取自己数组
+            byte[] getData = readInputStream(inputStream);
+
+            // 文件保存位置
+            File saveDir = new File(savePath);
+            if (!saveDir.exists()) {
+                saveDir.mkdirs();
+            }
+            String filePath = saveDir + File.separator + fileName;
+            String filePathFolder = filePath.substring(0, filePath.lastIndexOf("/") + 1);
+            FileUtil.mkdir(filePathFolder);
+
+            File file = new File(filePath);
+            fos = new FileOutputStream(file);
+            fos.write(getData);
+            if (fos != null) {
+                fos.close();
+            }
+            if (inputStream != null) {
+                inputStream.close();
+            }
+            System.out.println("info:" + url + " download success");
+        } catch(FileNotFoundException e){
+            return false;
+        } catch (IOException e) {
+            return false;
+        }finally {
+            if (fos != null) {
+                try {
+                    fos.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+            if (inputStream != null) {
+                try {
+                    inputStream.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+        }
+        return true;
+    }
+
+    /**
+     * 从输入流中获取字节数组
+     *
+     * @param inputStream
+     * @return
+     * @throws IOException
+     */
+    private static byte[] readInputStream(InputStream inputStream) throws IOException {
+        byte[] buffer = new byte[1024];
+        int len = 0;
+        ByteArrayOutputStream bos = new ByteArrayOutputStream();
+        while ((len = inputStream.read(buffer)) != -1) {
+            bos.write(buffer, 0, len);
+        }
+        bos.close();
+        return bos.toByteArray();
+    }
+
 }

+ 3 - 0
gis_service/src/main/java/com/gis/service/FormService.java

@@ -1,6 +1,7 @@
 package com.gis.service;
 
 
+import com.gis.common.util.Result;
 import com.gis.domain.dto.PageDto;
 import com.gis.domain.entity.FormEntity;
 
@@ -14,4 +15,6 @@ public interface FormService extends IBaseService<FormEntity, Long> {
 
 
     List<FormEntity> search(PageDto param);
+
+    Result exportQrCode();
 }

+ 0 - 33
gis_service/src/main/java/com/gis/service/impl/CraftServiceImpl.java

@@ -1,33 +0,0 @@
-package com.gis.service.impl;
-
-import com.gis.domain.dto.PageDto;
-import com.gis.domain.entity.FormEntity;
-import com.gis.mapper.IBaseMapper;
-import com.gis.mapper.FormMapper;
-import com.gis.service.FormService;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.stereotype.Service;
-
-import java.util.List;
-
-
-/**
- * Created by owen on 2020/3/11 0011 16:16
- */
-@Service
-public class CraftServiceImpl extends IBaseServiceImpl<FormEntity, Long> implements FormService {
-
-    @Autowired
-    private FormMapper entityMapper;
-
-    @Override
-    public IBaseMapper<FormEntity, Long> getBaseMapper() {
-        return this.entityMapper;
-    }
-
-
-    @Override
-    public List<FormEntity> search(PageDto param) {
-        return entityMapper.search(param);
-    }
-}

+ 69 - 0
gis_service/src/main/java/com/gis/service/impl/FormServiceImpl.java

@@ -0,0 +1,69 @@
+package com.gis.service.impl;
+
+import cn.hutool.core.date.DateUtil;
+import com.gis.common.constant.ConfigConstant;
+import com.gis.common.util.FileUtils;
+import com.gis.common.util.Result;
+import com.gis.domain.dto.PageDto;
+import com.gis.domain.entity.FormEntity;
+import com.gis.mapper.IBaseMapper;
+import com.gis.mapper.FormMapper;
+import com.gis.service.FormService;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import java.time.LocalDateTime;
+import java.util.List;
+
+
+/**
+ * Created by owen on 2020/3/11 0011 16:16
+ */
+@Slf4j
+@Service
+public class FormServiceImpl extends IBaseServiceImpl<FormEntity, Long> implements FormService {
+
+    @Autowired
+    private FormMapper entityMapper;
+
+    @Override
+    public IBaseMapper<FormEntity, Long> getBaseMapper() {
+        return this.entityMapper;
+    }
+
+    @Autowired
+    ConfigConstant configConstant;
+
+
+    @Override
+    public List<FormEntity> search(PageDto param) {
+        return entityMapper.search(param);
+    }
+
+    @Override
+    public Result exportQrCode() {
+        List<FormEntity> all = this.findAll();
+        if (all.size() == 0) {
+            return Result.failure("没有数据");
+        }
+        int i = 1;
+        String code = DateUtil.format(LocalDateTime.now(), "yyyyMMdd_HHmmssSSS");
+        String localBasePath = configConstant.serverBasePath +  "QrCode_" + code;
+        for (FormEntity entity : all) {
+            String qrCodeUrl = entity.getQrCodeUrl();
+            if (qrCodeUrl == null) {
+                continue;
+            }
+        // 网络下载二维码
+            String sampleName = entity.getSampleName();
+            sampleName = StringUtils.substringBeforeLast(sampleName,".");
+            log.info("网络下载文件地址 i: {} ,  {}", i, qrCodeUrl);
+            FileUtils.downLoadFromUrl(qrCodeUrl, sampleName + ".jpg", localBasePath);
+            log.info("第 {} 张下载完成, 地址: {}", i, localBasePath);
+            i ++ ;
+        }
+        return Result.success(i);
+    }
+}

+ 15 - 13
gis_web/src/main/java/com/gis/web/controller/BaseController.java

@@ -1,5 +1,6 @@
 package com.gis.web.controller;
 
+import com.gis.common.constant.ConfigConstant;
 import com.gis.domain.dto.PageDto;
 import com.gis.web.shiro.JwtUtil;
 import com.github.pagehelper.PageHelper;
@@ -17,20 +18,21 @@ public class BaseController {
     @Autowired
     protected HttpServletRequest request;
 
-    /**
-     * 服务器保存文件路径前缀
-     */
-    @Value("${file.path}")
-    public String FILE_PATH;
-//
-//    @Value("${server.domain}")
-//    public String SERVER_DOMAIN;
-
-    @Value("${oss.file.path}")
-    public String OSS_PATH;
+    @Autowired
+    ConfigConstant configConstant;
 
-    @Value("${oss.domain}")
-    public String OSS_DOMAIN;
+//    /**
+//     * 服务器保存文件路径前缀
+//     */
+//    @Value("${file.path}")
+//    public String FILE_PATH;
+//
+//
+//    @Value("${oss.file.path}")
+//    public String OSS_PATH;
+//
+//    @Value("${oss.domain}")
+//    public String OSS_DOMAIN;
 
 
 

+ 3 - 1
gis_web/src/main/java/com/gis/web/controller/FileController.java

@@ -30,7 +30,9 @@ public class FileController extends BaseController {
             log.error("文件不能为空");
             return Result.failure("文件不能为空");
         }
-        Object s = FileUtils.renameUploadOssMap(file, FILE_PATH, OSS_PATH, OSS_DOMAIN);
+//        Object s = FileUtils.renameUploadOssMap(file, FILE_PATH, OSS_PATH, OSS_DOMAIN);
+        Object s = FileUtils.renameUploadOssMap(
+                file, configConstant.serverBasePath, configConstant.ossBasePath, configConstant.ossDomain);
         return Result.success(s);
     }
 

+ 18 - 3
gis_web/src/main/java/com/gis/web/controller/FormController.java

@@ -36,6 +36,16 @@ public class FormController extends BaseController {
     private FormService formService;
 
 
+    /**
+     * 2021-06-21
+     */
+    @ApiOperation("批量导出二维码")
+    @GetMapping("exportQrCode")
+    public Result exportQrCode() {
+        return formService.exportQrCode();
+    }
+
+
     @ApiOperation("列表")
     @PostMapping("list")
     public Result<FormEntity> list(@RequestBody PageDto param) {
@@ -55,7 +65,9 @@ public class FormController extends BaseController {
 
             // 处理二维码, 没有示例表单不生成二维码
             if (StringUtils.isNotBlank(param.getSampleUrl())) {
-                String qrCodeUrl = QrCodeUtils.generateQrCode(param.getSampleUrl(), FILE_PATH, OSS_PATH, OSS_DOMAIN);
+//                String qrCodeUrl = QrCodeUtils.generateQrCode(param.getSampleUrl(), FILE_PATH, OSS_PATH, OSS_DOMAIN);
+                String qrCodeUrl = QrCodeUtils.generateQrCode(
+                        param.getSampleUrl(), configConstant.serverBasePath, configConstant.ossBasePath, configConstant.ossDomain);
                 entity.setQrCodeUrl(qrCodeUrl);
             }
 
@@ -72,7 +84,9 @@ public class FormController extends BaseController {
 
             // 处理二维码
             if (param.getChange() == 1) {
-                String qrCodeUrl = QrCodeUtils.generateQrCode(param.getSampleUrl(), FILE_PATH, OSS_PATH, OSS_DOMAIN);
+//                String qrCodeUrl = QrCodeUtils.generateQrCode(param.getSampleUrl(), FILE_PATH, OSS_PATH, OSS_DOMAIN);
+                String qrCodeUrl = QrCodeUtils.generateQrCode(
+                        param.getSampleUrl(), configConstant.serverBasePath, configConstant.ossBasePath, configConstant.ossDomain);
                 entity.setQrCodeUrl(qrCodeUrl);
             }
             entity.setUpdateTime(new Date());
@@ -126,7 +140,8 @@ public class FormController extends BaseController {
             log.error("文件不能为空");
             return Result.failure("文件不能为空");
         }
-        Object s = FileUtils.renameUploadOssMap(file, FILE_PATH, OSS_PATH, OSS_DOMAIN ,type, code);
+//        Object s = FileUtils.renameUploadOssMap(file, FILE_PATH, OSS_PATH, OSS_DOMAIN ,type, code);
+        Object s = FileUtils.renameUploadOssMap(file, configConstant.serverBasePath, configConstant.ossBasePath, configConstant.ossDomain ,type, code);
         return Result.success(s);
     }