diff --git a/src/main/java/com/cultural/heritage/controller/file/FileController.java b/src/main/java/com/cultural/heritage/controller/file/FileController.java index 8a75a29..679acad 100644 --- a/src/main/java/com/cultural/heritage/controller/file/FileController.java +++ b/src/main/java/com/cultural/heritage/controller/file/FileController.java @@ -6,11 +6,9 @@ import com.cultural.heritage.common.ErrorCode; import com.cultural.heritage.common.ResultUtils; import com.cultural.heritage.exception.BusinessException; import com.cultural.heritage.model.dto.file.UploadFileRequest; -import com.cultural.heritage.model.entity.User; import com.cultural.heritage.model.enums.FileUploadBizEnum; import com.cultural.heritage.service.file.IHweiYunOBSService; import com.cultural.heritage.service.user.UserService; -import com.cultural.heritage.utils.RegexUtils; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; import jakarta.annotation.Resource; @@ -39,62 +37,102 @@ public class FileController { private IHweiYunOBSService iHweiYunOBSService; + // 优化:设置一个合理的缓冲区大小 + private static final int BUFFER_SIZE = 8192; // 8 KB + + // 上传文件的存储目录 + private static final String UPLOAD_DIR = "/www/wwwroot/fileUpload/"; // 请替换为实际路径 + + + +// /** +// * Web端文件上传(obs桶) +// * @param multipartFile 文件对象 +// * @param uploadFileRequest 业务类型 +// * @return PutObjectResult +// */ +// @PostMapping("/uploadFile") +// @Operation(summary = "Web端文件上传(obs桶)", description = "参数:文件对象(multipartFile), 业务类型(biz),权限:管理员,方法名:uploadFile") +// public BaseResponse uploadFile(@RequestPart("file") MultipartFile multipartFile, UploadFileRequest uploadFileRequest, HttpServletRequest request) { +// // 校验用户是否登录 +// User loginUser = userService.getLoginUser(request); +// if (loginUser == null) { +// throw new BusinessException(ErrorCode.NOT_LOGIN_ERROR, "未登录"); +// } +// // 获取文件名 +// String filePath = getFilePath(multipartFile, uploadFileRequest); +// // 格式化图片特殊字符 +// filePath = RegexUtils.encodeUrl(filePath); +// // 上传文件至华为云服务器 +// iHweiYunOBSService.fileUpload(multipartFile, filePath); +// +// return ResultUtils.success(filePath, "上传成功"); +// } +// +// +// +// /** +// * 文件下载(obs桶) +// * @param objectKey 具体的文件名(含存储路径) +// */ +// @GetMapping("downloadFile") +// @Operation(summary = "文件下载(obs桶)", description = "参数:文件对象(具体的文件名(含存储路径)),权限:所有人,方法名:downloadFile") +// public void downloadFile(@RequestParam String objectKey, HttpServletResponse response) { +// iHweiYunOBSService.downloadFile(objectKey, response); +// } +// + + + /** - * Web端文件上传(obs桶) - * @param multipartFile 文件对象 - * @param uploadFileRequest 业务类型 - * @return PutObjectResult + * 文件上传(服务器) + * @param uploadFileRequest 业务请求 + * @param multipartFile 上传的文件 + * @return 上传文件的路径 */ - @PostMapping("/uploadFile") - @Operation(summary = "Web端文件上传(obs桶)", description = "参数:文件对象(multipartFile), 业务类型(biz),权限:管理员,方法名:uploadFile") - public BaseResponse uploadFile(@RequestPart("file") MultipartFile multipartFile, UploadFileRequest uploadFileRequest, HttpServletRequest request) { + @PostMapping("/upload") + @Operation(summary = "Web端文件上传(服务器)", description = "参数:文件对象(multipartFile), 业务类型(biz),权限:管理员,方法名:uploadFile") + public BaseResponse uploadFileToServe(@RequestParam("file") MultipartFile multipartFile, @RequestBody UploadFileRequest uploadFileRequest, HttpServletRequest request) throws IOException { + // 校验用户是否登录 - User loginUser = userService.getLoginUser(request); - if (loginUser == null) { - throw new BusinessException(ErrorCode.NOT_LOGIN_ERROR, "未登录"); - } - // 获取文件名 + userService.getLoginUser(request); + + // 校验文件 + validFile(multipartFile); + + // 获取文件的保存路径 String filePath = getFilePath(multipartFile, uploadFileRequest); - // 格式化图片特殊字符 - filePath = RegexUtils.encodeUrl(filePath); - // 上传文件至华为云服务器 - iHweiYunOBSService.fileUpload(multipartFile, filePath); + + // 创建上传目录,如果不存在 + File file = new File(UPLOAD_DIR + filePath); + if (!file.exists()) { + file.mkdirs(); // 如果路径不存在则创建 + } + // 将文件上传到目标位置 + try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file), BUFFER_SIZE)) { + bos.write(multipartFile.getBytes()); + } catch (IOException e) { + throw new BusinessException(ErrorCode.OPERATION_ERROR, "文件上传失败"); + } return ResultUtils.success(filePath, "上传成功"); } - /** - * 文件下载(obs桶) - * @param objectKey 具体的文件名(含存储路径) - */ - @GetMapping("downloadFile") - @Operation(summary = "文件下载(obs桶)", description = "参数:文件对象(具体的文件名(含存储路径)),权限:所有人,方法名:downloadFile") - public void downloadFile(@RequestParam String objectKey, HttpServletResponse response) { - iHweiYunOBSService.downloadFile(objectKey, response); - } - - - - - // 优化:设置一个合理的缓冲区大小 - private static final int BUFFER_SIZE = 8192; // 8 KB - /** * 文件下载(服务器) * @param filename 文件名 - * @param response 响应体 * @throws IOException */ @GetMapping("/download/{filename}") @Operation(summary = "文件下载(服务器)", description = "参数:文件名,权限:所有人,方法名:downloadFileFromServe") public void downloadFileFromServe(@PathVariable String filename, HttpServletResponse response) throws IOException { // 文件所在的路径 - String filePath = "/www/Fonts/" + filename; + String filePath = UPLOAD_DIR + filename; File file = new File(filePath); // 检查文件是否存在 @@ -143,10 +181,9 @@ public class FileController { /** * 校验文件 - * @param multipartFile 文件 - * @param fileUploadBizEnum 业务类型 + * @param multipartFile 文件 */ - private void validFile(MultipartFile multipartFile, FileUploadBizEnum fileUploadBizEnum) { + private void validFile(MultipartFile multipartFile) { // 文件大小 long fileSize = multipartFile.getSize(); // 文件后缀 @@ -176,7 +213,7 @@ public class FileController { throw new BusinessException(ErrorCode.PARAMS_ERROR, "业务名称错误"); } // 校验文件 - validFile(multipartFile, fileUploadBizEnum); + validFile(multipartFile); // 文件目录:根据业务、用户来划分 String uuid = RandomStringUtils.randomAlphabetic(8); String filename = uuid + "-" + multipartFile.getOriginalFilename();