From c04ad317cfba990dde689e66d36f3a46bc562064 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=92=B1=E5=93=A5=E4=B8=B6?= <7753810+zan_ge@user.noreply.gitee.com> Date: Mon, 2 Jan 2023 23:39:01 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E6=96=87=E4=BB=B6=E4=B8=8A?= =?UTF-8?q?=E4=BC=A0=E6=97=B6=E5=8F=AF=E5=A2=9E=E5=8A=A0=E8=BF=BD=E5=8A=A0?= =?UTF-8?q?=E8=87=AA=E5=AE=9A=E4=B9=89=E8=B7=AF=E5=BE=84=20=E5=A6=82aaa/?= =?UTF-8?q?=20,aaa/bbb/=20=E4=B8=8B=E8=BD=BD=E6=96=87=E4=BB=B6=E6=97=B6?= =?UTF-8?q?=E5=8F=AF=E4=BB=A5=E9=87=8D=E6=96=B0=E5=91=BD=E5=90=8D=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E7=9C=9F=E5=AE=9E=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../infra/controller/admin/file/FileController.java | 12 ++++++++++-- .../module/infra/service/file/FileServiceImpl.java | 6 ++---- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/file/FileController.java b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/file/FileController.java index 3719bc31b..a93e3367d 100644 --- a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/file/FileController.java +++ b/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") diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/file/FileServiceImpl.java b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/file/FileServiceImpl.java index 2226e7bae..dd0e339e0 100644 --- a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/file/FileServiceImpl.java +++ b/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;