Browse Source

批量下载

15050590500 3 years ago
parent
commit
6d2fc0d8b5

+ 5 - 0
src/main/java/com/gz/controller/archive/ArchiveFileController.java

@@ -31,6 +31,11 @@ public class ArchiveFileController {
         return archiveFileService.upload(file, archiveId, fileType,secondaryArchiveId);
     }
 
+    @PostMapping("/download/{archiveId}")
+    public void downloadAll(@PathVariable Integer archiveId) {
+        archiveFileService.downloadAll(archiveId);
+    }
+
     @DeleteMapping("delete")
     Integer delete(Integer id) {
         return archiveFileService.delete(id);

+ 2 - 0
src/main/java/com/gz/service/archive/ArchiveFileService.java

@@ -20,6 +20,8 @@ public interface ArchiveFileService {
      */
     ArchiveFileDTO upload(MultipartFile files,Integer archiveId, Integer fileType,Integer secondaryArchiveId) throws BusinessException, IOException;
 
+    void downloadAll(Integer archiveId);
+
     /**
      * @description 删除附件
      * @author LiuChangLan

+ 72 - 3
src/main/java/com/gz/service/archive/impl/ArchiveFileServiceImpl.java

@@ -11,10 +11,12 @@ import com.gz.config.FileUploadConfig;
 import com.gz.core.exception.BusinessException;
 import com.gz.core.exception.CustomExceptionEnum;
 import com.gz.dto.archive.ArchiveDTO;
+import com.gz.dto.archive.SecondaryArchiveDTO;
 import com.gz.dto.system.ArchiveFileDTO;
 import com.gz.dto.system.MenuRoleDTO;
 import com.gz.mapper.archive.ArchiveFileMapper;
 import com.gz.mapper.archive.ArchiveMapper;
+import com.gz.mapper.archive.SecondaryArchiveMapper;
 import com.gz.mapper.system.MenuRoleMapper;
 import com.gz.mapper.system.RoleMapper;
 import com.gz.rvo.archive.ArchiveFileRVO;
@@ -26,17 +28,21 @@ import com.gz.utils.WatermarkUtils;
 import com.gz.vo.archive.SearchArchiveFileVO;
 import com.gz.vo.rabbitmq.OcrEtlVO;
 import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
+import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
+import org.apache.commons.compress.utils.IOUtils;
 import org.springframework.amqp.rabbit.core.RabbitTemplate;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
+import org.springframework.util.StringUtils;
 import org.springframework.web.multipart.MultipartFile;
 
 import javax.annotation.Resource;
-import java.io.File;
-import java.io.IOException;
+import java.io.*;
 import java.lang.reflect.Executable;
+import java.util.ArrayList;
 import java.util.List;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
@@ -150,11 +156,74 @@ public class ArchiveFileServiceImpl implements ArchiveFileService {
             ocrEtlVO.setSecondaryArchiveId(secondaryArchiveId);
             ocrEtlVO.setFileUrl(archiveFileDTO.getFilePath() + "/" + archiveFileDTO.getFileName());
 //            ocrEtlVO.setFileUrl(serverFileUrlPrefix + archiveFileDTO.getFilePath() + "/" + archiveFileDTO.getFileName());
-            rabbitTemplate.convertAndSend(dataExchangeName, ocrBindingKey, JSON.toJSONString(ocrEtlVO));
+            // rabbitTemplate.convertAndSend(dataExchangeName, ocrBindingKey, JSON.toJSONString(ocrEtlVO));
         }
         return archiveFileDTO;
     }
 
+    /**
+     * 批量下载
+     */
+    @Override
+    public void downloadAll(Integer archiveId) {
+        ArchiveFileDTO dto = new ArchiveFileDTO();
+        dto.setArchiveId(archiveId);
+        List<ArchiveFileDTO> rvos = archiveFileMapper.select(dto);
+        if (rvos == null || rvos.isEmpty()) {
+            return;
+        }
+
+        List<String> filePaths = new ArrayList<>();
+        rvos.forEach(rvo -> {
+            if (StringUtils.hasText(rvo.getFilePath()) && StringUtils.hasText(rvo.getFileName())) {
+                filePaths.add(FileUploadConfig.ARCHIVE_FILE_ROOT_DIRECTORY + rvo.getFilePath() + rvo.getFileName());
+            }
+        });
+        ArchiveDTO archive = archiveMapper.selectByPrimaryKey(archiveId);
+        pack(filePaths, archive.getTm());
+    }
+
+    /**
+     * 打包tar压缩文件
+     */
+    public File pack(List<String> filePaths, String tarName) {
+        FileOutputStream out = null;
+        File target = null;
+        try {
+            // 存放路径
+            File parent = new File(FileUploadConfig.ARCHIVE_FILE_ROOT_DIRECTORY);
+            // 判断下载路径是否存在
+            if (!parent.exists()) {
+                parent.mkdirs();
+            }
+            target = new File(FileUploadConfig.ARCHIVE_FILE_ROOT_DIRECTORY + "/" + tarName + ".tar");
+            out = new FileOutputStream(target);
+        } catch (FileNotFoundException e1) {
+            e1.printStackTrace();
+        }
+        TarArchiveOutputStream os = new TarArchiveOutputStream(out);
+        for (String filePath : filePaths) {
+            File file = new File(filePath);
+            try {
+                TarArchiveEntry tarArchiveEntry = new TarArchiveEntry(file);
+
+                tarArchiveEntry.setName(file.getName());
+                os.putArchiveEntry(tarArchiveEntry);
+                IOUtils.copy(new FileInputStream(file), os);
+                os.closeArchiveEntry();
+            } catch (IOException e) {
+                e.printStackTrace();
+            }
+        }
+        try {
+            os.flush();
+            os.close();
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+        return target;
+    }
+
 
     @Override
     public Integer delete(Integer id) {