Commit c04ad317cfba990dde689e66d36f3a46bc562064
1 parent
7879632c
支持文件上传时可增加追加自定义路径 如aaa/ ,aaa/bbb/
下载文件时可以重新命名文件真实名
Showing
2 changed files
with
12 additions
and
6 deletions
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; | @@ -14,6 +14,7 @@ import cn.iocoder.yudao.module.infra.dal.dataobject.file.FileDO; | ||
| 14 | import cn.iocoder.yudao.module.infra.service.file.FileService; | 14 | import cn.iocoder.yudao.module.infra.service.file.FileService; |
| 15 | import io.swagger.annotations.Api; | 15 | import io.swagger.annotations.Api; |
| 16 | import io.swagger.annotations.ApiImplicitParam; | 16 | import io.swagger.annotations.ApiImplicitParam; |
| 17 | +import io.swagger.annotations.ApiImplicitParams; | ||
| 17 | import io.swagger.annotations.ApiOperation; | 18 | import io.swagger.annotations.ApiOperation; |
| 18 | import lombok.extern.slf4j.Slf4j; | 19 | import lombok.extern.slf4j.Slf4j; |
| 19 | import org.springframework.http.HttpStatus; | 20 | import org.springframework.http.HttpStatus; |
| @@ -61,9 +62,13 @@ public class FileController { | @@ -61,9 +62,13 @@ public class FileController { | ||
| 61 | @GetMapping("/{configId}/get/**") | 62 | @GetMapping("/{configId}/get/**") |
| 62 | @PermitAll | 63 | @PermitAll |
| 63 | @ApiOperation("下载文件") | 64 | @ApiOperation("下载文件") |
| 64 | - @ApiImplicitParam(name = "configId", value = "配置编号", required = true, dataTypeClass = Long.class) | 65 | + @ApiImplicitParams({ |
| 66 | + @ApiImplicitParam(name = "configId", value = "配置编号", required = true, dataTypeClass = Long.class), | ||
| 67 | + @ApiImplicitParam(name = "fileName", value = "文件实际名称", dataTypeClass = String.class) | ||
| 68 | + }) | ||
| 65 | public void getFileContent(HttpServletRequest request, | 69 | public void getFileContent(HttpServletRequest request, |
| 66 | HttpServletResponse response, | 70 | HttpServletResponse response, |
| 71 | + String fileName, | ||
| 67 | @PathVariable("configId") Long configId) throws Exception { | 72 | @PathVariable("configId") Long configId) throws Exception { |
| 68 | // 获取请求的路径 | 73 | // 获取请求的路径 |
| 69 | String path = StrUtil.subAfter(request.getRequestURI(), "/get/", false); | 74 | String path = StrUtil.subAfter(request.getRequestURI(), "/get/", false); |
| @@ -78,7 +83,10 @@ public class FileController { | @@ -78,7 +83,10 @@ public class FileController { | ||
| 78 | response.setStatus(HttpStatus.NOT_FOUND.value()); | 83 | response.setStatus(HttpStatus.NOT_FOUND.value()); |
| 79 | return; | 84 | return; |
| 80 | } | 85 | } |
| 81 | - ServletUtils.writeAttachment(response, path, content); | 86 | + if (StrUtil.isBlank(fileName)) { |
| 87 | + fileName = StrUtil.subAfter(path, "/", true); | ||
| 88 | + } | ||
| 89 | + ServletUtils.writeAttachment(response, fileName, content); | ||
| 82 | } | 90 | } |
| 83 | 91 | ||
| 84 | @GetMapping("/page") | 92 | @GetMapping("/page") |
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 { | @@ -39,11 +39,9 @@ public class FileServiceImpl implements FileService { | ||
| 39 | @Override | 39 | @Override |
| 40 | @SneakyThrows | 40 | @SneakyThrows |
| 41 | public String createFile(String name, String path, byte[] content) { | 41 | public String createFile(String name, String path, byte[] content) { |
| 42 | - // 计算默认的 path 名 | 42 | + // 计算默认的 path 名,path可增加自定义路径如 aaa/,aaa/bbb/ |
| 43 | String type = FileTypeUtils.getMineType(content, name); | 43 | String type = FileTypeUtils.getMineType(content, name); |
| 44 | - if (StrUtil.isEmpty(path)) { | ||
| 45 | - path = FileUtils.generatePath(content, name); | ||
| 46 | - } | 44 | + path = StrUtil.isEmpty(path) ? FileUtils.generatePath(content, name) : path + FileUtils.generatePath(content, name); |
| 47 | // 如果 name 为空,则使用 path 填充 | 45 | // 如果 name 为空,则使用 path 填充 |
| 48 | if (StrUtil.isEmpty(name)) { | 46 | if (StrUtil.isEmpty(name)) { |
| 49 | name = path; | 47 | name = path; |