From 1bc4ba76b20f06b2932c1bc20948a6d88d40ab2b Mon Sep 17 00:00:00 2001 From: Yoofff Date: Wed, 19 Jun 2024 18:39:25 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BC=98=E5=8C=96=E5=85=A8=E5=B1=80?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E4=B8=8A=E4=BC=A0=E5=BC=82=E5=B8=B8-?= =?UTF-8?q?=E8=B6=85=E8=BF=87=E4=B8=8A=E4=BC=A0=E5=A4=A7=E5=B0=8F=E9=99=90?= =?UTF-8?q?=E5=88=B6=E7=9A=84=E5=BC=82=E5=B8=B8=E5=88=A4=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../exception/GlobalExceptionHandler.java | 27 ++++++++++++++++--- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/continew-starter-web/src/main/java/top/continew/starter/web/autoconfigure/exception/GlobalExceptionHandler.java b/continew-starter-web/src/main/java/top/continew/starter/web/autoconfigure/exception/GlobalExceptionHandler.java index 98398eb5..24e8cdfc 100644 --- a/continew-starter-web/src/main/java/top/continew/starter/web/autoconfigure/exception/GlobalExceptionHandler.java +++ b/continew-starter-web/src/main/java/top/continew/starter/web/autoconfigure/exception/GlobalExceptionHandler.java @@ -33,6 +33,7 @@ import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RestControllerAdvice; import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException; import org.springframework.web.multipart.MaxUploadSizeExceededException; +import org.springframework.web.multipart.MultipartException; import top.continew.starter.core.constant.StringConstants; import top.continew.starter.core.exception.BadRequestException; import top.continew.starter.core.exception.BusinessException; @@ -108,11 +109,29 @@ public class GlobalExceptionHandler { /** * 拦截文件上传异常-超过上传大小限制 */ - @ExceptionHandler(MaxUploadSizeExceededException.class) - public R handleMaxUploadSizeExceededException(MaxUploadSizeExceededException e, HttpServletRequest request) { - log.warn("请求地址 [{}],上传文件失败,文件大小超过限制。", request.getRequestURI(), e); - String sizeLimit = CharSequenceUtil.subBetween(e.getMessage(), "The maximum size ", " for"); + @ExceptionHandler(MultipartException.class) + public R handleRequestTooBigException(MultipartException e, HttpServletRequest request) { + String msg = e.getMessage(); + R defaultFail = R.fail(HttpStatus.BAD_REQUEST.value(), msg); + if (CharSequenceUtil.isBlank(msg)) { + return defaultFail; + } + + String sizeLimit; + Throwable cause = e.getCause(); + if (null != cause) { + msg = msg.concat(cause.getMessage().toLowerCase()); + } + if (msg.contains("size") && msg.contains("exceed")) { + sizeLimit = CharSequenceUtil.subBetween(msg, "maximum (", ")"); + } else if (msg.contains("larger than")) { + sizeLimit = CharSequenceUtil.subAfter(msg, "larger than ", true); + } else { + return defaultFail; + } + String errorMsg = "请上传小于 %sMB 的文件".formatted(NumberUtil.parseLong(sizeLimit) / 1024 / 1024); + log.warn("请求地址 [{}],上传文件失败,文件大小超过限制。", request.getRequestURI(), e); return R.fail(HttpStatus.BAD_REQUEST.value(), errorMsg); }