修复:调整后端上传大小限制
This commit is contained in:
+44
@@ -2,6 +2,8 @@ package com.xiaowang.yuminailcore.common;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.context.support.DefaultMessageSourceResolvable;
|
||||
import org.springframework.web.multipart.MaxUploadSizeExceededException;
|
||||
import org.springframework.web.multipart.MultipartException;
|
||||
import org.springframework.validation.BindException;
|
||||
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
@@ -19,6 +21,8 @@ import javax.validation.ConstraintViolationException;
|
||||
@RestControllerAdvice
|
||||
public class GlobalExceptionHandler {
|
||||
|
||||
private static final long MAX_UPLOAD_SIZE_BYTES = 5L * 1024 * 1024;
|
||||
|
||||
@ExceptionHandler(MethodArgumentNotValidException.class)
|
||||
public Result<Void> handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
|
||||
String message = e.getBindingResult().getAllErrors().stream()
|
||||
@@ -46,6 +50,28 @@ public class GlobalExceptionHandler {
|
||||
return Result.error(message);
|
||||
}
|
||||
|
||||
@ExceptionHandler(MaxUploadSizeExceededException.class)
|
||||
public Result<Void> handleMaxUploadSizeExceededException(MaxUploadSizeExceededException e) {
|
||||
log.warn("上传文件超过限制: {}", e.getMessage());
|
||||
return Result.error(buildUploadLimitMessage());
|
||||
}
|
||||
|
||||
@ExceptionHandler(MultipartException.class)
|
||||
public Result<Void> handleMultipartException(MultipartException e) {
|
||||
log.warn("文件上传请求异常: {}", e.getMessage());
|
||||
return Result.error(buildUploadLimitMessage());
|
||||
}
|
||||
|
||||
@ExceptionHandler(IllegalStateException.class)
|
||||
public Result<Void> handleIllegalStateException(IllegalStateException e) {
|
||||
if (isUploadLimitException(e)) {
|
||||
log.warn("上传文件超过限制: {}", e.getMessage());
|
||||
return Result.error(buildUploadLimitMessage());
|
||||
}
|
||||
log.error("非法状态异常: ", e);
|
||||
return Result.error(e.getMessage());
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理运行时异常
|
||||
*/
|
||||
@@ -63,4 +89,22 @@ public class GlobalExceptionHandler {
|
||||
log.error("系统异常: ", e);
|
||||
return Result.error("系统繁忙,请稍后再试");
|
||||
}
|
||||
|
||||
private String buildUploadLimitMessage() {
|
||||
return "上传文件不能超过 " + (MAX_UPLOAD_SIZE_BYTES / 1024 / 1024) + "MB";
|
||||
}
|
||||
|
||||
private boolean isUploadLimitException(Throwable throwable) {
|
||||
Throwable current = throwable;
|
||||
while (current != null) {
|
||||
String message = current.getMessage();
|
||||
if (message != null && (message.contains("maximum permitted size")
|
||||
|| message.contains("FileSizeLimitExceededException")
|
||||
|| message.contains("exceeds its maximum permitted size"))) {
|
||||
return true;
|
||||
}
|
||||
current = current.getCause();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
spring:
|
||||
datasource:
|
||||
url: jdbc:mysql://localhost:3306/yumi_nail?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
|
||||
servlet:
|
||||
multipart:
|
||||
max-file-size: 5MB
|
||||
max-request-size: 10MB
|
||||
jackson:
|
||||
time-zone: Asia/Shanghai
|
||||
date-format: yyyy-MM-dd HH:mm:ss
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
# 2026-03-26 后端上传大小限制调整为 5MB
|
||||
|
||||
## 问题
|
||||
|
||||
- 后台上传作品图片时,后端仍使用 Spring 默认的 `1MB` multipart 限制。
|
||||
- 当前前端已经支持“单张图片超过 `5MB` 自动压缩后上传”,但服务端限制未同步,导致上传阶段直接报错:
|
||||
`The field file exceeds its maximum permitted size of 1048576 bytes`
|
||||
|
||||
## 本次调整
|
||||
|
||||
- 调整 [application.yaml](/E:/code/xiaowang/YumiNail-Core/src/main/resources/application.yaml)
|
||||
- 新增 `spring.servlet.multipart.max-file-size: 5MB`
|
||||
- 新增 `spring.servlet.multipart.max-request-size: 10MB`
|
||||
- 这样单文件上传口径与前端保持一致,且保留请求体额外开销余量。
|
||||
|
||||
- 调整 [GlobalExceptionHandler.java](/E:/code/xiaowang/YumiNail-Core/src/main/java/com/xiaowang/yuminailcore/common/GlobalExceptionHandler.java)
|
||||
- 新增对 `MaxUploadSizeExceededException` 的处理。
|
||||
- 新增对 `MultipartException` 的处理。
|
||||
- 新增对上传超限场景下 `IllegalStateException` 的兜底识别。
|
||||
- 前端收到的提示统一改为中文:`上传文件不能超过 5MB`。
|
||||
|
||||
## 验证
|
||||
|
||||
- 已执行后端编译:`mvn -q -DskipTests compile`
|
||||
- 编译结果通过。
|
||||
|
||||
## 说明
|
||||
|
||||
- 本轮没有数据库变更。
|
||||
- 如果 `YumiNail-Dev` 命名空间里的 `yuminail-core` 配置后续显式写入了 `spring.servlet.multipart.*`,需要保持与本次代码中的 `5MB / 10MB` 口径一致,避免再次被外部配置覆盖。
|
||||
Reference in New Issue
Block a user