Ver Fonte

新增下载单个文件和批量打包下载的接口

王苗苗 há 4 dias atrás
pai
commit
48f065e030

+ 88 - 0
slibra-admin/src/main/java/com/slibra/web/controller/business/FileDownloadController.java

@@ -0,0 +1,88 @@
+package com.slibra.web.controller.business;
+
+import org.springframework.core.io.ByteArrayResource;
+import org.springframework.core.io.InputStreamResource;
+import org.springframework.core.io.Resource;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.MediaType;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.bind.annotation.*;
+
+import java.io.*;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.util.List;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipOutputStream;
+
+@RestController
+@RequestMapping("/api/files")
+public class FileDownloadController {
+
+    //目录
+    private static final String FILE_STORAGE_LOCATION = "/Users/wangmiaomiao/Downloads/mlvrwj.20250217/";
+
+    @GetMapping("/download")
+    public ResponseEntity<InputStreamResource> downloadFile(@RequestParam String fileName) throws IOException {
+        // 构造文件的完整路径
+        File file = new File(FILE_STORAGE_LOCATION + fileName);
+
+        // 检查文件是否存在
+        if (!file.exists()) {
+            throw new RuntimeException("File not found " + fileName);
+        }
+
+        // 读取文件内容到字节数组
+        byte[] fileData = Files.readAllBytes(Paths.get(file.getAbsolutePath()));
+
+        // 设置HTTP响应头
+        HttpHeaders header = new HttpHeaders();
+        header.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + file.getName() + "\"");
+        header.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_OCTET_STREAM_VALUE);
+
+        // 返回文件内容作为响应体
+        return ResponseEntity.ok()
+                .headers(header)
+                .body(new InputStreamResource(new ByteArrayInputStream(fileData)));
+    }
+
+
+    @PostMapping("/downloadBatch")
+    public ResponseEntity<Resource> downloadBatchFiles(@RequestBody List<String> fileNames) throws IOException {
+        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
+        try (ZipOutputStream zipOutputStream = new ZipOutputStream(byteArrayOutputStream)) {
+            for (String fileName : fileNames) {
+                File file = new File(FILE_STORAGE_LOCATION + fileName);
+                if (!file.exists()) {
+                    throw new RuntimeException("File not found: " + fileName);
+                }
+                addFileToZip(file, file.getName(), zipOutputStream);
+            }
+        }
+
+        byte[] zipContent = byteArrayOutputStream.toByteArray();
+        ByteArrayResource resource = new ByteArrayResource(zipContent);
+
+        HttpHeaders headers = new HttpHeaders();
+        headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"batch_download.zip\"");
+        headers.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_OCTET_STREAM_VALUE);
+
+        return ResponseEntity.ok()
+                .headers(headers)
+                .contentLength(zipContent.length)
+                .body(resource);
+    }
+
+    private void addFileToZip(File file, String entryName, ZipOutputStream zipOut) throws IOException {
+        try (FileInputStream fis = new FileInputStream(file)) {
+            ZipEntry zipEntry = new ZipEntry(entryName);
+            zipOut.putNextEntry(zipEntry);
+
+            byte[] bytes = new byte[1024];
+            int length;
+            while ((length = fis.read(bytes)) >= 0) {
+                zipOut.write(bytes, 0, length);
+            }
+        }
+    }
+}