From 904728ef597190fdc7d92e14313e83bbbc6ee613 Mon Sep 17 00:00:00 2001 From: liming Date: Sun, 10 Dec 2023 20:31:12 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E9=99=84=E4=BB=B6=E4=B8=8A?= =?UTF-8?q?=E4=BC=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/FileUploadController.java | 50 +++++++++++++++++++ .../common/exception/NonCaptureException.java | 13 +++++ .../web/exception/GlobalExceptionHandler.java | 11 ++++ ruoyi-ui/src/utils/generator/config.js | 2 +- 4 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 ruoyi-admin/src/main/java/com/ruoyi/web/controller/common/FileUploadController.java create mode 100644 ruoyi-common/src/main/java/com/ruoyi/common/exception/NonCaptureException.java diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/common/FileUploadController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/common/FileUploadController.java new file mode 100644 index 00000000..e5dd3939 --- /dev/null +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/common/FileUploadController.java @@ -0,0 +1,50 @@ +package com.ruoyi.web.controller.common; + +import com.ruoyi.common.annotation.Anonymous; +import com.ruoyi.common.config.RuoYiConfig; +import com.ruoyi.common.core.domain.AjaxResult; +import com.ruoyi.common.exception.NonCaptureException; +import com.ruoyi.common.utils.StringUtils; +import com.ruoyi.common.utils.file.FileUploadUtils; +import com.ruoyi.common.utils.file.FileUtils; +import com.ruoyi.framework.config.ServerConfig; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.multipart.MultipartFile; + +@RestController +@RequestMapping("/file") +public class FileUploadController { + + private final Logger log = LoggerFactory.getLogger(FileUploadController.class); + + @Autowired + private ServerConfig serverConfig; + + @Anonymous + @PostMapping("/upload") + @SuppressWarnings("DuplicatedCode") + public AjaxResult uploadFile(MultipartFile file) { + try { + log.info("文件 {} 上传中...", file.getOriginalFilename()); + // 上传文件路径 + String filePath = RuoYiConfig.getUploadPath(); + // 上传并返回新文件名称 + String fileName = FileUploadUtils.upload(filePath, file); + String url = serverConfig.getUrl() + fileName; + AjaxResult ajax = AjaxResult.success(); + ajax.put("url", url); + ajax.put("fileName", fileName); + ajax.put("newFileName", FileUtils.getName(fileName)); + ajax.put("originalFilename", file.getOriginalFilename()); + log.info("文件 {} 上传成功!", file.getOriginalFilename()); + return ajax; + } catch (Exception e) { + throw new NonCaptureException(StringUtils.format("文件 {} 上传失败!", file.getOriginalFilename()), e); + } + } +} \ No newline at end of file diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/exception/NonCaptureException.java b/ruoyi-common/src/main/java/com/ruoyi/common/exception/NonCaptureException.java new file mode 100644 index 00000000..a2ecb76b --- /dev/null +++ b/ruoyi-common/src/main/java/com/ruoyi/common/exception/NonCaptureException.java @@ -0,0 +1,13 @@ +package com.ruoyi.common.exception; + +/** + * 不进行捕获的异常 + * + * @author liming + * @date 2023/12/10 20:11 + */ +public class NonCaptureException extends RuntimeException { + public NonCaptureException(String message, Throwable cause) { + super(message, cause); + } +} diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/web/exception/GlobalExceptionHandler.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/web/exception/GlobalExceptionHandler.java index d4d64212..9e145115 100644 --- a/ruoyi-framework/src/main/java/com/ruoyi/framework/web/exception/GlobalExceptionHandler.java +++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/web/exception/GlobalExceptionHandler.java @@ -1,6 +1,8 @@ package com.ruoyi.framework.web.exception; import javax.servlet.http.HttpServletRequest; + +import com.ruoyi.common.exception.NonCaptureException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.security.access.AccessDeniedException; @@ -135,4 +137,13 @@ public class GlobalExceptionHandler { return AjaxResult.error("演示模式,不允许操作"); } + + /** + * 不进行捕获的异常 + */ + @ExceptionHandler(NonCaptureException.class) + public AjaxResult handleNonCaptureException(NonCaptureException e) + { + throw e; + } } diff --git a/ruoyi-ui/src/utils/generator/config.js b/ruoyi-ui/src/utils/generator/config.js index dd8d5906..8795335f 100644 --- a/ruoyi-ui/src/utils/generator/config.js +++ b/ruoyi-ui/src/utils/generator/config.js @@ -499,7 +499,7 @@ export const selectComponents = [ __slot__: { 'list-type': true }, - action: 'https://jsonplaceholder.typicode.com/posts/', + action: 'http://localhost:8080/file/upload', disabled: false, accept: '', name: 'file',