第一种
@RestController @RequestMapping("/download") public class DownloadController { @GetMapping("/files") public ResponseEntity<InputStreamResource> downloadFiles() throws IOException { // 创建临时ZIP文件 Path zipFilePath = Files.createTempFile("files", ".zip"); // 需要打包的文件路径 String[] filePaths = { "D:/QQ截图20240520102047.jpg", "D:/人员考勤记录.xlsx" }; // 打包文件 try (ZipOutputStream zos = new ZipOutputStream(Files.newOutputStream(zipFilePath))) { for (String filePath : filePaths) { File file = new File(filePath); try (FileInputStream fis = new FileInputStream(file)) { ZipEntry zipEntry = new ZipEntry(file.getName()); zos.putNextEntry(zipEntry); byte[] buffer = new byte[1024]; int len; while ((len = fis.read(buffer)) > 0) { zos.write(buffer, 0, len); } zos.closeEntry(); } } } // 返回ZIP文件 InputStreamResource resource = new InputStreamResource(Files.newInputStream(zipFilePath)); HttpHeaders headers = new HttpHeaders(); headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename="+URLEncoder.encode("files.zip","UTF-8")); return ResponseEntity .ok() .headers(headers) .contentType(MediaType.APPLICATION_OCTET_STREAM) .body(resource); } }
第二种与MINIO文件服务器集成,inputStream这里需要改成自己的Api
@GetMapping("/download/files") public ResponseEntity<InputStreamResource> downloadFiles(@RequestParam List<String> fileTokens) throws IOException { // 获取文件信息列表 // List<FileEntity> fileEntities = fileRepository.findByFileTokenIn(fileTokens); // LambdaQueryWrapper<SysOss> sysOssLambdaQueryWrapper = Wrappers.lambdaQuery(SysOss.class).in(SysOss::getFileToken, fileTokens); GetFileInfoParam getFileInfoParam = new GetFileInfoParam(); getFileInfoParam.setF8s(fileTokens); List<SysOssVO> ossVOList = iFileDownloadClient.getFileByToken(getFileInfoParam).getData(); if (ossVOList.isEmpty()) { return null; } // 创建字节数组输出流用于打包ZIP文件 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); try (ZipOutputStream zos = new ZipOutputStream(byteArrayOutputStream)) { for (SysOssVO ossVO : ossVOList) { // String bucketName = fileEntity.getBucketName(); // String objectName = fileEntity.getObjectName(); // String fileName = fileEntity.getFileName(); // 从MinIO获取文件输入流 /*try (var inputStream = minioClient.getObject( GetObjectArgs.builder() .bucket(bucketName) .object(objectName) .build())) {*/ try (var inputStream = iFileDownloadClient.getAttachmentStream(ossVO.getFileToken())) { // 将文件写入ZIP文件 ZipEntry zipEntry = new ZipEntry(ossVO.getFileName()); zos.putNextEntry(zipEntry); byte[] buffer = new byte[1024]; int len; while ((len = inputStream.read(buffer)) > 0) { zos.write(buffer, 0, len); } zos.closeEntry(); } } } // 返回ZIP文件 InputStreamResource resource = new InputStreamResource(new ByteArrayInputStream(byteArrayOutputStream.toByteArray())); HttpHeaders headers = new HttpHeaders(); headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename="+URLEncoder.encode("files.zip","UTF-8")); return ResponseEntity .ok() .headers(headers) .contentType(MediaType.APPLICATION_OCTET_STREAM) .body(resource); }
标签:文件,SpringBoot,len,headers,zos,打包,new,HttpHeaders,下载 From: https://www.cnblogs.com/Jack-zhao/p/18244225