diff --git a/laws-module-system/src/main/java/com/jero/modules/oss/service/IOSSFileService.java b/laws-module-system/src/main/java/com/jero/modules/oss/service/IOSSFileService.java index d8cbed9a..0f0646a5 100644 --- a/laws-module-system/src/main/java/com/jero/modules/oss/service/IOSSFileService.java +++ b/laws-module-system/src/main/java/com/jero/modules/oss/service/IOSSFileService.java @@ -26,4 +26,13 @@ public interface IOSSFileService extends IService { void updateFileInfo(List ossFileList); OSSFile uploadLocalOfCos(MultipartFile mf, String bizPath, String state, String cut); + + + /** + * 获取文件内容 + * @param file + * @return + * @throws Exception + */ + byte[] getMinioFileContent(OSSFile file) throws Exception; } diff --git a/laws-module-system/src/main/java/com/jero/modules/oss/service/impl/OSSFileServiceImpl.java b/laws-module-system/src/main/java/com/jero/modules/oss/service/impl/OSSFileServiceImpl.java index 19f291b8..785864af 100644 --- a/laws-module-system/src/main/java/com/jero/modules/oss/service/impl/OSSFileServiceImpl.java +++ b/laws-module-system/src/main/java/com/jero/modules/oss/service/impl/OSSFileServiceImpl.java @@ -19,7 +19,9 @@ import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; +import java.io.ByteArrayOutputStream; import java.io.IOException; +import java.io.InputStream; import java.util.ArrayList; import java.util.Arrays; import java.util.Date; @@ -330,4 +332,26 @@ public class OSSFileServiceImpl extends ServiceImpl impl oSSFile.setUrl(null); return oSSFile; } + + @Override + public byte[] getMinioFileContent(OSSFile file) throws Exception { + String fileUrl = file.getUrl(); + String minioUrl = MinioUtil.getMinioUrl(); + minioUrl = minioUrl + MinioUtil.getBucketName() + "/"; + String url = fileUrl.replace(minioUrl, ""); + + try (InputStream inputStream = MinioUtil.getMinioFile(MinioUtil.getBucketName(), url); + ByteArrayOutputStream outputStream = new ByteArrayOutputStream() + ) { + byte[] buf = new byte[1024]; + int len; + while ((len = inputStream.read(buf)) > 0) { + outputStream.write(buf, 0, len); + } + return outputStream.toByteArray(); + } catch (Exception e) { + log.error(e.getMessage()); + throw e; + } + } } diff --git a/laws-modules/src/main/java/com/jero/modules/laws/common/service/impl/LawsCommonServiceImpl.java b/laws-modules/src/main/java/com/jero/modules/laws/common/service/impl/LawsCommonServiceImpl.java index ffa16f61..13d8a189 100644 --- a/laws-modules/src/main/java/com/jero/modules/laws/common/service/impl/LawsCommonServiceImpl.java +++ b/laws-modules/src/main/java/com/jero/modules/laws/common/service/impl/LawsCommonServiceImpl.java @@ -1059,7 +1059,7 @@ public class LawsCommonServiceImpl implements ILawsCommonService { zipOut.putNextEntry(fileEntry); // 将文件取出存入流中 - bytes = getMinioFileContent(ossFile); + bytes = ossFileService.getMinioFileContent(ossFile); zipOut.write(bytes, 0, bytes.length); zipOut.closeEntry(); } @@ -1071,29 +1071,6 @@ public class LawsCommonServiceImpl implements ILawsCommonService { } } - private byte[] getMinioFileContent(OSSFile file) throws Exception { - String fileUrl = file.getUrl(); - String minioUrl = MinioUtil.getMinioUrl(); - minioUrl = minioUrl + MinioUtil.getBucketName() + "/"; - String url = fileUrl.replace(minioUrl, ""); - - try (InputStream inputStream = MinioUtil.getMinioFile(MinioUtil.getBucketName(), url); - ByteArrayOutputStream outputStream = new ByteArrayOutputStream() - ) { - byte[] buf = new byte[1024]; - int len; - while ((len = inputStream.read(buf)) > 0) { - outputStream.write(buf, 0, len); - } - return outputStream.toByteArray(); - } catch (Exception e) { - log.error(e.getMessage()); - throw e; - } - } - - - /** * @Author: liao * @Date: 2023/8/24 9:53 diff --git a/laws-modules/src/main/java/com/jero/modules/laws/enterprise/controller/EnterpriseStandardDeclareController.java b/laws-modules/src/main/java/com/jero/modules/laws/enterprise/controller/EnterpriseStandardDeclareController.java index 62f01952..cc49fea4 100644 --- a/laws-modules/src/main/java/com/jero/modules/laws/enterprise/controller/EnterpriseStandardDeclareController.java +++ b/laws-modules/src/main/java/com/jero/modules/laws/enterprise/controller/EnterpriseStandardDeclareController.java @@ -16,6 +16,7 @@ import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import javax.servlet.http.HttpServletResponse; +import java.util.Map; /** @@ -55,4 +56,11 @@ public class EnterpriseStandardDeclareController extends JeroController queryPageList(ESQueryCommonDTO esDeclare, Integer pageNo, Integer pageSize) { IPage page = new Page<>(pageNo, pageSize); @@ -65,6 +76,80 @@ public class EnterpriseStandardDeclareServiceImpl extends ServiceImpl enterpriseStandardDeclareVoIPage = this.queryPageList(esQueryCommonDTO, 1, Integer.MAX_VALUE); + List records = enterpriseStandardDeclareVoIPage.getRecords(); + // 设置文件字段 + this.setRecordFile(records); + List enterpriseStandardDeclareExPorts = BeanCopyUtils.copyBeanList(records, EnterpriseStandardDeclareExPortDto.class); + + // 设置导出参数 + ExportParams exportParams = new ExportParams(null, sheetName); // 可以为标题添加更多的配置 + Workbook workbook = ExcelExportUtil.exportExcel(exportParams, EnterpriseStandardDeclareExPortDto.class, enterpriseStandardDeclareExPorts); + + // 设置响应头和内容类型 + response.setContentType("application/vnd.ms-excel"); + response.setHeader("Content-Disposition", "attachment; filename=" + fileName + ".xls"); + + // 使用 ByteArrayOutputStream 来捕获 Excel 数据 + ByteArrayOutputStream excelOutputStream = new ByteArrayOutputStream(); + workbook.write(excelOutputStream); + workbook.close(); + + // 准备ZIP + // 获取编码后的文件名 + String encodedZipName = URLEncoder.encode(fileName + ".zip", "UTF-8"); + response.setContentType("application/zip"); + response.setHeader("Content-Disposition", "attachment; filename=" + encodedZipName); + response.setHeader("Content-Transfer-Encoding", "binary"); + response.setHeader("Pragma", "public"); + response.setHeader("Cache-Control", "no-store"); + response.setHeader("Expires", "0"); + ZipOutputStream zipOut = new ZipOutputStream(response.getOutputStream()); + + // 将Excel加入ZIP + ZipEntry excelEntry = new ZipEntry(fileName + ".xls"); + zipOut.putNextEntry(excelEntry); + byte[] bytes = excelOutputStream.toByteArray(); + zipOut.write(bytes, 0, bytes.length); + zipOut.closeEntry(); + + List fileIds = records.stream().map(EnterpriseStandardDeclareVo::getDeclareFile).collect(Collectors.toList()); + LambdaQueryWrapper ossFileLambdaQueryWrapper = new LambdaQueryWrapper<>(); + ossFileLambdaQueryWrapper.in(OSSFile::getId, fileIds); + List files = ossFileService.list(ossFileLambdaQueryWrapper); + Map ossFileMap = files.stream().collect(Collectors.toMap(OSSFile::getId, ossFile -> ossFile)); + + // 将文件和文件夹加入ZIP + for (EnterpriseStandardDeclareVo vo : records) { + OSSFile ossFile = ossFileMap.get(vo.getDeclareFile()); + ZipEntry fileEntry = new ZipEntry(vo.getDeclareFileName()); + zipOut.putNextEntry(fileEntry); + + // 将文件取出存入流中 + bytes = ossFileService.getMinioFileContent(ossFile); + zipOut.write(bytes, 0, bytes.length); + zipOut.closeEntry(); + } + zipOut.close(); + } catch (Exception e) { + log.error("导出zip异常", e); + throw new JeroBootException("导出zip异常", e); + } + } + + + /** + * 设置导出的Excel中的相关文件字段 + * @param records + */ + private void setRecordFile(List records) { + records.forEach(record -> record.setDeclareFileName(record.getStandardNo().replace("/", "_") + "/" + record.getDeclareFileName())); + } } diff --git a/laws-modules/src/main/java/com/jero/modules/laws/enterprise/util/BeanCopyUtils.java b/laws-modules/src/main/java/com/jero/modules/laws/enterprise/util/BeanCopyUtils.java new file mode 100644 index 00000000..f987a6b6 --- /dev/null +++ b/laws-modules/src/main/java/com/jero/modules/laws/enterprise/util/BeanCopyUtils.java @@ -0,0 +1,36 @@ +package com.jero.modules.laws.enterprise.util; + +import org.springframework.beans.BeanUtils; + +import java.util.List; +import java.util.stream.Collectors; + +/** + * @author Mzaxd + * @since 2022-11-22 15:36 + */ +public class BeanCopyUtils { + + private BeanCopyUtils() { + } + + public static V copyBean(Object source, Class clazz){ + V result = null; + //创建目标对象 + try { + result = clazz.newInstance(); + //实现属性copy + BeanUtils.copyProperties(source, result); + } catch (Exception e) { + e.printStackTrace(); + } + //返回结果 + return result; + } + + public static List copyBeanList(List list, Class clazz) { + return list.stream() + .map(o -> copyBean(o, clazz)) + .collect(Collectors.toList()); + } +}