Browse Source

支持文件上传时可增加追加自定义路径 如aaa/ ,aaa/bbb/
下载文件时可以重新命名文件真实名

咱哥丶 2 years ago
parent
commit
c04ad317cf

+ 10 - 2
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/file/FileController.java

@@ -14,6 +14,7 @@ import cn.iocoder.yudao.module.infra.dal.dataobject.file.FileDO;
 import cn.iocoder.yudao.module.infra.service.file.FileService;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiImplicitParam;
+import io.swagger.annotations.ApiImplicitParams;
 import io.swagger.annotations.ApiOperation;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.http.HttpStatus;
@@ -61,9 +62,13 @@ public class FileController {
     @GetMapping("/{configId}/get/**")
     @PermitAll
     @ApiOperation("下载文件")
-    @ApiImplicitParam(name = "configId", value = "配置编号",  required = true, dataTypeClass = Long.class)
+    @ApiImplicitParams({
+            @ApiImplicitParam(name = "configId", value = "配置编号", required = true, dataTypeClass = Long.class),
+            @ApiImplicitParam(name = "fileName", value = "文件实际名称", dataTypeClass = String.class)
+    })
     public void getFileContent(HttpServletRequest request,
                                HttpServletResponse response,
+                               String fileName,
                                @PathVariable("configId") Long configId) throws Exception {
         // 获取请求的路径
         String path = StrUtil.subAfter(request.getRequestURI(), "/get/", false);
@@ -78,7 +83,10 @@ public class FileController {
             response.setStatus(HttpStatus.NOT_FOUND.value());
             return;
         }
-        ServletUtils.writeAttachment(response, path, content);
+        if (StrUtil.isBlank(fileName)) {
+            fileName = StrUtil.subAfter(path, "/", true);
+        }
+        ServletUtils.writeAttachment(response, fileName, content);
     }
 
     @GetMapping("/page")

+ 2 - 4
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/file/FileServiceImpl.java

@@ -39,11 +39,9 @@ public class FileServiceImpl implements FileService {
     @Override
     @SneakyThrows
     public String createFile(String name, String path, byte[] content) {
-        // 计算默认的 path 名
+        // 计算默认的 path 名,path可增加自定义路径如 aaa/,aaa/bbb/
         String type = FileTypeUtils.getMineType(content, name);
-        if (StrUtil.isEmpty(path)) {
-            path = FileUtils.generatePath(content, name);
-        }
+        path = StrUtil.isEmpty(path) ? FileUtils.generatePath(content, name) : path + FileUtils.generatePath(content, name);
         // 如果 name 为空,则使用 path 填充
         if (StrUtil.isEmpty(name)) {
             name = path;