Эх сурвалжийг харах

增加文件下载通用接口

wukai 1 жил өмнө
parent
commit
675328f612

+ 18 - 5
doc-biz/src/main/java/com/doc/biz/controller/ApiController.java

@@ -52,13 +52,26 @@ public class ApiController extends BaseController {
     @ApiOperation("文件预览")
     @GetMapping("/access/{fileId}")
     public ResponseEntity<Object> access(@PathVariable(name = "fileId") String fileId) {
+        return down(fileId, false);
+    }
+
+    /**
+     * 文件下载
+     *
+     * @param fileId fileId
+     * @return
+     */
+    @ApiOperation("文件下载")
+    @GetMapping("/download/{fileId}")
+    public ResponseEntity<Object> download(@PathVariable(name = "fileId") String fileId) {
+        return down(fileId, true);
+    }
+
+    private ResponseEntity<Object> down(String fileId, boolean down) {
         DocumentVO vo = mongoService.downloadFile(fileId);
         if (Objects.nonNull(vo)) {
-            return ResponseEntity.ok()
-                    .header(HttpHeaders.CONTENT_DISPOSITION, "filename=\"" + UriEncoder.encode(vo.getFileName()) + "\"")
-                    .header(HttpHeaders.CONTENT_TYPE, vo.getContentType())
-                    .header(HttpHeaders.CONTENT_LENGTH, vo.getFileSize() + "").header("Connection", "close")
-                    .body(vo.getData());
+            String disposition = down ? "attachment;" : "" + "filename=\"" + UriEncoder.encode(vo.getFileName()) + "\"";
+            return ResponseEntity.ok().header(HttpHeaders.CONTENT_DISPOSITION, disposition).header(HttpHeaders.CONTENT_TYPE, vo.getContentType()).header(HttpHeaders.CONTENT_LENGTH, vo.getFileSize() + "").header("Connection", "close").body(vo.getData());
         } else {
             return ResponseEntity.status(HttpStatus.NOT_FOUND).body("file does not exist");
         }