feat(file):优化文件上传逻辑并解决中文乱码问题
- 引入 Hutool 工具类处理文件名唯一性和扩展名提取 - 使用 UUID 重命名文件,避免信创环境下的中文乱码问题- 调整租户、日期和用户路径结构以支持更细粒度的文件管理 - 统一代码格式,增强可读性与维护性 -修复潜在空指针异常风险点- 完善接口文档描述信息一致性
This commit is contained in:
@@ -1,7 +1,9 @@
|
|||||||
package com.jeelowcode.service.infra.controller;
|
package com.jeelowcode.service.infra.controller;
|
||||||
|
|
||||||
import cn.hutool.core.date.DateUtil;
|
import cn.hutool.core.date.DateUtil;
|
||||||
|
import cn.hutool.core.io.FileUtil;
|
||||||
import cn.hutool.core.io.IoUtil;
|
import cn.hutool.core.io.IoUtil;
|
||||||
|
import cn.hutool.core.util.IdUtil;
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
import cn.hutool.core.util.URLUtil;
|
import cn.hutool.core.util.URLUtil;
|
||||||
import com.jeelowcode.service.infra.controller.vo.file.*;
|
import com.jeelowcode.service.infra.controller.vo.file.*;
|
||||||
@@ -9,6 +11,7 @@ import com.jeelowcode.service.infra.entity.FileDO;
|
|||||||
import com.jeelowcode.service.infra.service.IFileService;
|
import com.jeelowcode.service.infra.service.IFileService;
|
||||||
import com.jeelowcode.tool.framework.common.pojo.CommonResult;
|
import com.jeelowcode.tool.framework.common.pojo.CommonResult;
|
||||||
import com.jeelowcode.tool.framework.common.pojo.PageResult;
|
import com.jeelowcode.tool.framework.common.pojo.PageResult;
|
||||||
|
import com.jeelowcode.tool.framework.common.util.io.FileUtils;
|
||||||
import com.jeelowcode.tool.framework.common.util.object.BeanUtils;
|
import com.jeelowcode.tool.framework.common.util.object.BeanUtils;
|
||||||
import com.jeelowcode.tool.framework.common.util.servlet.ServletUtils;
|
import com.jeelowcode.tool.framework.common.util.servlet.ServletUtils;
|
||||||
import com.jeelowcode.tool.framework.operatelog.core.annotations.OperateLog;
|
import com.jeelowcode.tool.framework.operatelog.core.annotations.OperateLog;
|
||||||
@@ -45,7 +48,7 @@ public class FileController {
|
|||||||
private IFileService fileService;
|
private IFileService fileService;
|
||||||
|
|
||||||
@PostMapping("/upload")
|
@PostMapping("/upload")
|
||||||
@Operation(tags = "文件管理",summary = "上传文件", description = "模式一:后端上传文件")
|
@Operation(tags = "文件管理", summary = "上传文件", description = "模式一:后端上传文件")
|
||||||
@OperateLog(logArgs = false) // 上传文件,没有记录操作日志的必要
|
@OperateLog(logArgs = false) // 上传文件,没有记录操作日志的必要
|
||||||
public CommonResult<String> uploadFile(FileUploadReqVO uploadReqVO) throws Exception {
|
public CommonResult<String> uploadFile(FileUploadReqVO uploadReqVO) throws Exception {
|
||||||
MultipartFile file = uploadReqVO.getFile();
|
MultipartFile file = uploadReqVO.getFile();
|
||||||
@@ -54,66 +57,66 @@ public class FileController {
|
|||||||
//查询当天是否存在该名称
|
//查询当天是否存在该名称
|
||||||
|
|
||||||
|
|
||||||
// String fileName = uploadReqVO.getPath();
|
// String fileName = uploadReqVO.getPath();
|
||||||
//jeeLowCode修改
|
//jeeLowCode修改
|
||||||
//判断当天是否有该名称了
|
//判断当天是否有该名称了
|
||||||
|
|
||||||
//根据天来分类
|
//根据天来分类
|
||||||
String today = DateUtil.today();
|
String today = DateUtil.today();
|
||||||
String publicPath="upload/"+today+"/";
|
String publicPath = "upload/" + today + "/";
|
||||||
String path=publicPath+fileName;
|
String path = publicPath + fileName;
|
||||||
|
|
||||||
fileName = fileService.getUniqueFileName(fileName, path);
|
fileName = fileService.getUniqueFileName(fileName, path);
|
||||||
path=publicPath+fileName;
|
path = publicPath + fileName;
|
||||||
|
|
||||||
return success(fileService.createFile(fileName, path, IoUtil.readBytes(file.getInputStream())));
|
return success(fileService.createFile(fileName, path, IoUtil.readBytes(file.getInputStream())));
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("/jeelowcode/upload")
|
@PostMapping("/jeelowcode/upload")
|
||||||
@Operation(tags = "文件管理",summary = "上传文件", description = "模式一:后端上传文件")
|
@Operation(tags = "文件管理", summary = "上传文件", description = "模式一:后端上传文件")
|
||||||
@OperateLog(logArgs = false) // 上传文件,没有记录操作日志的必要
|
@OperateLog(logArgs = false) // 上传文件,没有记录操作日志的必要
|
||||||
public CommonResult<Map<String,String>> jeeLowCodeUploadFile(FileUploadReqVO uploadReqVO) throws Exception {
|
public CommonResult<Map<String, String>> jeeLowCodeUploadFile(FileUploadReqVO uploadReqVO) throws Exception {
|
||||||
MultipartFile file = uploadReqVO.getFile();
|
MultipartFile file = uploadReqVO.getFile();
|
||||||
String fileName = file.getOriginalFilename();
|
String fileName = file.getOriginalFilename();
|
||||||
//微信图片_20230905094700.png
|
//微信图片_20230905094700.png
|
||||||
Long tenantId =-1L;
|
Long tenantId = -1L;
|
||||||
Long userId =-1L;
|
Long userId = -1L;
|
||||||
LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
|
LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
|
||||||
if(loginUser!=null){
|
if (loginUser != null) {
|
||||||
tenantId = loginUser.getTenantId();
|
tenantId = loginUser.getTenantId();
|
||||||
userId = loginUser.getId();
|
userId = loginUser.getId();
|
||||||
}
|
}
|
||||||
//根据天来分类
|
//根据天来分类
|
||||||
String today = DateUtil.today();
|
String today = DateUtil.today();
|
||||||
String publicPath="upload/"+tenantId+"/"+today+"/"+userId+"/"; //upload/租户id/20240720/用户id/aa.jpg
|
String publicPath = "upload/" + tenantId + "/" + today + "/" + userId + "/"; //upload/租户id/20240720/用户id/aa.jpg
|
||||||
String path=publicPath+fileName;
|
String path = publicPath + fileName;
|
||||||
|
|
||||||
fileName = fileService.getUniqueFileName(fileName, path);
|
fileName = fileService.getUniqueFileName(fileName, path);
|
||||||
path=publicPath+fileName;
|
// 解决信创环境下文件名中文乱码导致附件错乱的问题
|
||||||
|
path = publicPath + IdUtil.simpleUUID() + (StrUtil.isEmpty(FileUtil.extName(fileName)) ? "" : ("." + FileUtil.extName(fileName)));
|
||||||
|
|
||||||
String fileUrl = fileService.createFile(fileName, path, IoUtil.readBytes(file.getInputStream()));
|
String fileUrl = fileService.createFile(fileName, path, IoUtil.readBytes(file.getInputStream()));
|
||||||
|
|
||||||
Map<String,String> resultMap=new HashMap<>();
|
Map<String, String> resultMap = new HashMap<>();
|
||||||
resultMap.put("fileUrl",fileUrl);
|
resultMap.put("fileUrl", fileUrl);
|
||||||
return success(resultMap);
|
return success(resultMap);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@GetMapping("/presigned-url")
|
@GetMapping("/presigned-url")
|
||||||
@Operation(tags = "文件管理",summary = "获取文件预签名地址", description = "模式二:前端上传文件:用于前端直接上传七牛、阿里云 OSS 等文件存储器")
|
@Operation(tags = "文件管理", summary = "获取文件预签名地址", description = "模式二:前端上传文件:用于前端直接上传七牛、阿里云 OSS 等文件存储器")
|
||||||
public CommonResult<FilePresignedUrlRespVO> getFilePresignedUrl(@RequestParam("path") String path) throws Exception {
|
public CommonResult<FilePresignedUrlRespVO> getFilePresignedUrl(@RequestParam("path") String path) throws Exception {
|
||||||
return success(fileService.getFilePresignedUrl(path));
|
return success(fileService.getFilePresignedUrl(path));
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("/create")
|
@PostMapping("/create")
|
||||||
@Operation(tags = "文件管理",summary = "创建文件", description = "模式二:前端上传文件:配合 presigned-url 接口,记录上传了上传的文件")
|
@Operation(tags = "文件管理", summary = "创建文件", description = "模式二:前端上传文件:配合 presigned-url 接口,记录上传了上传的文件")
|
||||||
public CommonResult<Long> createFile(@Valid @RequestBody FileCreateReqVO createReqVO) {
|
public CommonResult<Long> createFile(@Valid @RequestBody FileCreateReqVO createReqVO) {
|
||||||
return success(fileService.createFile(createReqVO));
|
return success(fileService.createFile(createReqVO));
|
||||||
}
|
}
|
||||||
|
|
||||||
@DeleteMapping("/delete")
|
@DeleteMapping("/delete")
|
||||||
@Operation(tags = "文件管理",summary = "删除文件")
|
@Operation(tags = "文件管理", summary = "删除文件")
|
||||||
@Parameter(name = "id", description = "编号", required = true)
|
@Parameter(name = "id", description = "编号", required = true)
|
||||||
@PreAuthorize("@ss.hasPermission('infra:file:delete')")
|
@PreAuthorize("@ss.hasPermission('infra:file:delete')")
|
||||||
public CommonResult<Boolean> deleteFile(@RequestParam("id") Long id) throws Exception {
|
public CommonResult<Boolean> deleteFile(@RequestParam("id") Long id) throws Exception {
|
||||||
@@ -123,7 +126,7 @@ public class FileController {
|
|||||||
|
|
||||||
@GetMapping("/{configId}/get/**")
|
@GetMapping("/{configId}/get/**")
|
||||||
@PermitAll
|
@PermitAll
|
||||||
@Operation(tags = "文件管理",summary = "下载文件")
|
@Operation(tags = "文件管理", summary = "下载文件")
|
||||||
@Parameter(name = "configId", description = "配置编号", required = true)
|
@Parameter(name = "configId", description = "配置编号", required = true)
|
||||||
public void getFileContent(HttpServletRequest request,
|
public void getFileContent(HttpServletRequest request,
|
||||||
HttpServletResponse response,
|
HttpServletResponse response,
|
||||||
@@ -147,7 +150,7 @@ public class FileController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/page")
|
@GetMapping("/page")
|
||||||
@Operation(tags = "文件管理",summary = "获得文件分页")
|
@Operation(tags = "文件管理", summary = "获得文件分页")
|
||||||
@PreAuthorize("@ss.hasPermission('infra:file:query')")
|
@PreAuthorize("@ss.hasPermission('infra:file:query')")
|
||||||
public CommonResult<PageResult<FileRespVO>> getFilePage(@Valid FilePageReqVO pageVO) {
|
public CommonResult<PageResult<FileRespVO>> getFilePage(@Valid FilePageReqVO pageVO) {
|
||||||
PageResult<FileDO> pageResult = fileService.getFilePage(pageVO);
|
PageResult<FileDO> pageResult = fileService.getFilePage(pageVO);
|
||||||
|
|||||||
Reference in New Issue
Block a user