Procházet zdrojové kódy

质量反馈增加打包下载

FengChaoYu před 3 měsíci
rodič
revize
a9fa8d54e3

+ 14 - 0
mall-server-api/src/main/java/com/gree/mall/manager/controller/quality/QualityFeedbackController.java

@@ -23,6 +23,7 @@ import org.springframework.web.bind.annotation.*;
 import javax.annotation.Resource;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
 import java.util.List;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.locks.Lock;
@@ -116,4 +117,17 @@ public class QualityFeedbackController {
             obtain.unlock();
         }
     }
+
+
+    /**
+     * 压缩远程服务器文件
+     */
+    @GetMapping("/down/zip")
+    @ApiOperation("下载附件")
+    public void downloadZip(
+            @ApiParam(value = "id", required = true) @RequestParam String id,
+            HttpServletResponse response
+    )  throws Exception {
+        qualityFeedbackLogic.downZipFile(id, response);
+    }
 }

+ 30 - 3
mall-server-api/src/main/java/com/gree/mall/manager/logic/quality/QualityFeedbackLogic.java

@@ -3,12 +3,14 @@ package com.gree.mall.manager.logic.quality;
 import cn.hutool.core.collection.CollectionUtil;
 import cn.hutool.core.date.DateUtil;
 import com.baomidou.mybatisplus.core.metadata.IPage;
+import com.baomidou.mybatisplus.core.toolkit.IdWorker;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.gree.mall.manager.bean.admin.AdminUserCom;
 import com.gree.mall.manager.bean.quality.QualityFeedbackBean;
 import com.gree.mall.manager.bean.quality.QualityFeedbackCountBean;
 import com.gree.mall.manager.bean.quality.QualityFeedbackVO;
 import com.gree.mall.manager.commonmapper.CommonMapper;
+import com.gree.mall.manager.enums.base.BaseEnum;
 import com.gree.mall.manager.enums.quality.ExamineQualityFeedbackStatusEnum;
 import com.gree.mall.manager.enums.quality.QualityFeedbackTypeEnum;
 import com.gree.mall.manager.exception.RemoteServiceException;
@@ -18,6 +20,8 @@ import com.gree.mall.manager.plus.entity.QualityFeedbackItem;
 import com.gree.mall.manager.plus.service.AdminWebsitService;
 import com.gree.mall.manager.plus.service.QualityFeedbackItemService;
 import com.gree.mall.manager.plus.service.QualityFeedbackService;
+import com.gree.mall.manager.utils.CompressDownUtil;
+import com.gree.mall.manager.utils.oss.OSSUtil;
 import com.gree.mall.manager.zfire.bean.ZfireParamBean;
 import com.gree.mall.manager.zfire.util.FieldUtils;
 import lombok.RequiredArgsConstructor;
@@ -26,9 +30,8 @@ import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.BeanUtils;
 import org.springframework.stereotype.Service;
 
-import java.util.List;
-import java.util.Map;
-import java.util.Objects;
+import javax.servlet.http.HttpServletResponse;
+import java.util.*;
 import java.util.function.Function;
 import java.util.stream.Collectors;
 
@@ -42,6 +45,11 @@ public class QualityFeedbackLogic {
     private final QualityFeedbackService qualityFeedbackService;
     private final QualityFeedbackItemService qualityFeedbackItemService;
     private final AdminWebsitService adminWebsitService;
+    private final OSSUtil ossUtil;
+
+    private static final String DOWN_PATH = "classpath:tmp/quality/";
+
+    private final String FILE_SUFFIX = ".zip";
 
     public IPage<QualityFeedbackVO> list(ZfireParamBean zfireParamBean) {
         AdminUserCom adminUser = commonLogic.getAdminUser();
@@ -269,4 +277,23 @@ public class QualityFeedbackLogic {
                 .ne(QualityFeedback::getStatus, ExamineQualityFeedbackStatusEnum.SAVE.getKey())
                 .update();
     }
+
+    public void downZipFile(String id, HttpServletResponse response) throws Exception {
+        QualityFeedback qualityFeedback = qualityFeedbackService.getById(id);
+
+        List<QualityFeedbackItem> dataList = qualityFeedbackItemService.lambdaQuery()
+                .eq(QualityFeedbackItem::getQualityFeedbckId, id)
+                .list();
+        List<Map<String, String>> list = new ArrayList<>();
+        for (QualityFeedbackItem data : dataList) {
+            String filePath = data.getUrl();
+            Map<String, String> map = new HashMap<>();
+            map.put("path", filePath);
+            map.put("fileName", BaseEnum.keyToValueOpt(QualityFeedbackTypeEnum.class, data.getType()).orElse(IdWorker.getIdStr()) + IdWorker.getId());
+            list.add(map);
+        }
+        String downloadName = qualityFeedback.getTitle() + FILE_SUFFIX;
+        CompressDownUtil.setDownloadResponse(response, downloadName);
+        CompressDownUtil.compressZipByName(list, response.getOutputStream(),ossUtil,commonLogic);
+    }
 }

+ 195 - 0
mall-server-api/src/main/java/com/gree/mall/manager/utils/CompressDownUtil.java

@@ -0,0 +1,195 @@
+package com.gree.mall.manager.utils;
+
+import com.gree.mall.manager.logic.common.CommonLogic;
+import com.gree.mall.manager.utils.oss.OSSUtil;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.http.HttpResponse;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.impl.client.CloseableHttpClient;
+
+import javax.servlet.http.HttpServletResponse;
+import java.io.*;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipOutputStream;
+
+@Slf4j
+public class CompressDownUtil {
+
+    private static final int  BUFFER_SIZE =  1024;
+
+    private CompressDownUtil() {
+    }
+
+    /**
+     * 设置下载响应头
+     */
+    public static HttpServletResponse setDownloadResponse(HttpServletResponse response, String downloadName) throws UnsupportedEncodingException {
+        response.reset();
+        response.setCharacterEncoding("utf-8");
+        response.setContentType("application/octet-stream");
+        response.setHeader("Content-Disposition", "attachment; filename=" + new String(downloadName.getBytes("utf-8"),"ISO-8859-1"));
+        // 这些是对请求头的匹配
+        response.setHeader("Access-Control-Allow-Origin","*");
+        response.setHeader("Access-Control-Allow-Credentials","true");
+        response.setHeader("Access-Control-Allow-Methods","GET, HEAD, POST, PUT, DELETE, OPTIONS, PATCH");
+        // 这行代码添加相应的的允许字段
+        response.setHeader("Access-Control-Allow-Headers","authorization, content-type,x-token");
+        response.setHeader("Access-Control-Expose-Headers","X-forwared-port, X-forwarded-host");
+        response.setHeader("Vary","Origin,Access-Control-Request-Method,Access-Control-Request-Headers");
+
+
+        return response;
+    }
+
+    public static Integer[] toIntegerArray(String param) {
+        return Arrays.stream(param.split(","))
+                .map(Integer::valueOf)
+                .toArray(Integer[]::new);
+    }
+
+    /**
+     * 将多个文件压缩到指定输出流中
+     *
+     * @param files        需要压缩的文件列表
+     * @param outputStream 压缩到指定的输出流
+     */
+    public static void compressZip(List<File> files, OutputStream outputStream) {
+        // 包装成ZIP格式输出流
+        try (ZipOutputStream zipOutStream = new ZipOutputStream(new BufferedOutputStream(outputStream))) {
+            // 设置压缩方法
+            zipOutStream.setMethod(ZipOutputStream.DEFLATED);
+            // 将多文件循环写入压缩包
+            for (int i = 0; i < files.size(); i++) {
+                File file = files.get(i);
+                FileInputStream filenputStream = new FileInputStream(file);
+                byte[] data = new byte[(int) file.length()];
+                filenputStream.read(data);
+                // 添加ZipEntry,并ZipEntry中写入文件流,这里,加上i是防止要下载的文件有重名的导致下载失败
+                zipOutStream.putNextEntry(new ZipEntry(i + "-" + file.getName()));
+                zipOutStream.write(data);
+                filenputStream.close();
+                zipOutStream.closeEntry();
+            }
+        } catch (IOException e) {
+            log.error(CompressDownUtil.class.getName(), "downloadallfiles", e);
+        } finally {
+            try {
+                if (Objects.nonNull(outputStream)) {
+                    outputStream.close();
+                }
+            } catch (IOException e) {
+                log.error(CompressDownUtil.class.getName(), "downloadallfiles", e);
+            }
+        }
+    }
+
+    private static HttpResponse invokeGetFile(String url, CloseableHttpClient httpclient) {
+
+        HttpResponse response;
+        try {
+            HttpGet get = new HttpGet(url);
+            get.setHeader("Content-type", "UTF-8");
+            get.setHeader("Connection", "close");
+            response = httpclient.execute(get);
+
+        } catch (Exception e) {
+            log.error("请求文件异常");
+            return null;
+        }
+        return response;
+    }
+
+
+
+    public static void compressZipByName(List<Map<String,String>> files, OutputStream outputStream, OSSUtil ossUtil, CommonLogic commonLogic) {
+        // 包装成ZIP格式输出流
+        try (ZipOutputStream zipOutStream = new ZipOutputStream(new BufferedOutputStream(outputStream))) {
+            //  设置压缩方法
+            zipOutStream.setMethod(ZipOutputStream.DEFLATED);
+            // 将多文件循环写入压缩包
+            for (int i = 0; i < files.size(); i++) {
+                String url = ossUtil.getUrl(files.get(i).get("path"));
+                InputStream inputStream = commonLogic.getFileInputStreamByUrl(url);
+                byte [] content = new byte[BUFFER_SIZE];
+                int len;
+                zipOutStream.putNextEntry(new ZipEntry(i+"-"+files.get(i).get("fileName")));
+                while((len=inputStream.read(content))!=-1){
+                    zipOutStream.write(content,0,len);
+                   // zipOutStream.flush();
+                }
+                zipOutStream.closeEntry();
+                if (inputStream != null) {
+                    inputStream.close();
+                }
+            }
+        } catch (IOException e) {
+            log.error(CompressDownUtil.class.getName(), "downloadallfiles", e);
+        } finally {
+            try {
+                if (Objects.nonNull(outputStream)) {
+                    outputStream.close();
+                }
+            } catch (IOException e) {
+                log.error(CompressDownUtil.class.getName(), "downloadallfiles", e);
+            }
+        }
+    }
+
+
+
+    /**
+     * 下载文件
+     *
+     * @param outputStream 下载输出流
+     * @param zipFilePath  需要下载文件的路径
+     */
+    public static void downloadFile(OutputStream outputStream, String zipFilePath) {
+        File zipFile = new File(zipFilePath);
+        if (!zipFile.exists()) {
+            // 需要下载压塑包文件不存在
+            return;
+        }
+        try (FileInputStream inputStream = new FileInputStream(zipFile)) {
+            byte[] data = new byte[(int) zipFile.length()];
+            inputStream.read(data);
+            outputStream.write(data);
+            outputStream.flush();
+        } catch (IOException e) {
+            log.error(CompressDownUtil.class.getName(), "downloadZip", e);
+        } finally {
+            try {
+                if (Objects.nonNull(outputStream)) {
+                    outputStream.close();
+                }
+            } catch (IOException e) {
+                log.error(CompressDownUtil.class.getName(), "downloadZip", e);
+            }
+        }
+    }
+
+    /**
+     * 删除指定路径的文件
+     *
+     * @param filepath 文件路径
+     */
+    public static void deleteFile(String filepath) {
+        File file = new File(filepath);
+        deleteFile(file);
+    }
+
+    /**
+     * 删除指定文件
+     *
+     * @param file 文件
+     */
+    public static void deleteFile(File file) {
+        // 路径为文件且不为空则进行删除
+        if (file.isFile() && file.exists()) {
+            file.delete();
+        }
+    }
+}