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 | 14 | import cn.iocoder.yudao.module.infra.service.file.FileService; |
| 15 | 15 | import io.swagger.annotations.Api; |
| 16 | 16 | import io.swagger.annotations.ApiImplicitParam; |
| 17 | +import io.swagger.annotations.ApiImplicitParams; | |
| 17 | 18 | import io.swagger.annotations.ApiOperation; |
| 18 | 19 | import lombok.extern.slf4j.Slf4j; |
| 19 | 20 | import org.springframework.http.HttpStatus; |
| ... | ... | @@ -61,9 +62,13 @@ public class FileController { |
| 61 | 62 | @GetMapping("/{configId}/get/**") |
| 62 | 63 | @PermitAll |
| 63 | 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 | 69 | public void getFileContent(HttpServletRequest request, |
| 66 | 70 | HttpServletResponse response, |
| 71 | + String fileName, | |
| 67 | 72 | @PathVariable("configId") Long configId) throws Exception { |
| 68 | 73 | // 获取请求的路径 |
| 69 | 74 | String path = StrUtil.subAfter(request.getRequestURI(), "/get/", false); |
| ... | ... | @@ -78,7 +83,10 @@ public class FileController { |
| 78 | 83 | response.setStatus(HttpStatus.NOT_FOUND.value()); |
| 79 | 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 | 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 | 39 | @Override |
| 40 | 40 | @SneakyThrows |
| 41 | 41 | public String createFile(String name, String path, byte[] content) { |
| 42 | - // 计算默认的 path 名 | |
| 42 | + // 计算默认的 path 名,path可增加自定义路径如 aaa/,aaa/bbb/ | |
| 43 | 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 | 45 | // 如果 name 为空,则使用 path 填充 |
| 48 | 46 | if (StrUtil.isEmpty(name)) { |
| 49 | 47 | name = path; | ... | ... |