jinlin
2023-12-15 da2330bb7951c602ebe72a9f53ff13d0895b3349
core/src/main/java/com/zt/life/sys/service/SysOssConfigService.java
@@ -13,16 +13,27 @@
import com.zt.core.oss.service.ISysOssConfigService;
import com.zt.core.oss.service.ISysOssService;
import com.zt.life.core.constant.Cache;
import com.zt.life.export.service.DownloadService;
import com.zt.life.oss.OssEncryptService;
import com.zt.life.sys.dao.SysOssConfigDao;
import com.zt.life.sys.dto.OssDto;
import com.zt.life.sys.mapstruct.OssExMapper;
import com.zt.life.sys.model.SysOssConfig;
import com.zt.life.util.ZipCipherUtil;
import com.zt.modules.oss.model.SysOss;
import com.zt.modules.oss.service.SysOssService;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
import java.util.stream.Collectors;
@@ -42,6 +53,15 @@
    @Autowired
    private SysOssService sysOssServices;
    @Value("${zt.oss.local-path}")
    private String localPath;
    @Value("${data.key}")
    private String key;
    @Autowired
    private OssEncryptService ossEncryptService;
    /**
     * 分页查询
@@ -320,4 +340,62 @@
        return baseDao.selectList(new QueryWrapper<SysOssConfig>().eq(Constant.TableColumn.IS_DELETE, Constant.Bool.NO).lambda()
                .eq(SysOssConfig::getBusiType, busiType));
    }
    public void downloadFilesByosList(HttpServletRequest request, HttpServletResponse response, List<SysOss> list, String projectName) {
        try {
            String uuid = UUIDUtil.getUUID();
            String remoteFileFullPath = Paths.get(localPath, "temp", uuid).toString();
            Files.createDirectories(Paths.get(remoteFileFullPath));
            for (SysOss so : list) {
                try {
                    String localFileFullPath = Paths.get(localPath, so.getPath()).toString();
                    File localFile = new File(localFileFullPath);
                    if (localFile.exists()) {
                        try (InputStream in = ossEncryptService.decrypt(localFile);
                             BufferedInputStream bis = new BufferedInputStream(in);
                             BufferedOutputStream bos = new BufferedOutputStream(
                                     new FileOutputStream(remoteFileFullPath + File.separator + so.getName()))) {
                            byte[] buffer = new byte[1024];
                            int len;
                            while ((len = bis.read(buffer)) != -1) {
                                bos.write(buffer, 0, len);
                            }
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace(); // 或者记录日志
                }
            }
            String fileNameZip = Paths.get(localPath, "temp", uuid + ".zip").toString();
            new ZipCipherUtil().encryptZip(remoteFileFullPath, fileNameZip, key);
            File zipFile = new File(fileNameZip);
            response.reset();
            response.setContentLength((int) zipFile.length());
            response.setContentType("application/octet-stream;charset=UTF-8");
            String encodedFilename = DownloadService.getNameEncoder(request, projectName + ".zip");
            response.addHeader("Content-Disposition", "attachment;filename=" + encodedFilename);
            try (FileInputStream fileInputStream = new FileInputStream(zipFile);
                 ServletOutputStream outputStream = response.getOutputStream()) {
                byte[] buffers = new byte[1024];
                int length;
                while ((length = fileInputStream.read(buffers)) > 0) {
                    outputStream.write(buffers, 0, length);
                }
            } catch (IOException e) {
                e.printStackTrace(); // 或者记录日志
            } finally {
                FileUtils.deleteQuietly(new File(remoteFileFullPath));
                FileUtils.deleteQuietly(new File(fileNameZip));// 删除临时文件夹及其内容
            }
        } catch (Exception ex) {
            ex.printStackTrace(); // 或者记录日志
        }
    }
}