支持文件上传时可增加追加自定义路径 如aaa/ ,aaa/bbb/

下载文件时可以重新命名文件真实名
This commit is contained in:
咱哥丶 2023-01-02 23:39:01 +08:00
parent 7879632cf5
commit c04ad317cf
2 changed files with 12 additions and 6 deletions

View File

@ -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")

View File

@ -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;