/admin-api/projects/{projectCode}/uploads/commit
用户端和管理端表单项上限设为 1003,支持每批 500 个文件。 数量超限单独返回 UPLOAD_TOO_MANY_PARTS,补充排查日志。
This commit is contained in:
+22
-1
@@ -1,7 +1,9 @@
|
||||
package com.linkerbot.vibemake.common;
|
||||
|
||||
import jakarta.validation.ConstraintViolationException;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
|
||||
import org.apache.tomcat.util.http.fileupload.impl.FileCountLimitExceededException;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.http.MediaType;
|
||||
@@ -19,6 +21,9 @@ import org.springframework.web.multipart.MaxUploadSizeExceededException;
|
||||
import org.springframework.web.multipart.support.MissingServletRequestPartException;
|
||||
import org.springframework.web.servlet.resource.NoResourceFoundException;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@RestControllerAdvice
|
||||
public class GlobalExceptionHandler {
|
||||
private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);
|
||||
@@ -51,7 +56,23 @@ public class GlobalExceptionHandler {
|
||||
}
|
||||
|
||||
@ExceptionHandler(MaxUploadSizeExceededException.class)
|
||||
public ResponseEntity<ApiResponse<Object>> oversized(Exception error) {
|
||||
public ResponseEntity<ApiResponse<Object>> oversized(
|
||||
MaxUploadSizeExceededException error, HttpServletRequest request) {
|
||||
// Spring 也将 Tomcat 的表单项数量超限包装成大小异常,必须按原因区分。
|
||||
List<String> causes = new ArrayList<>();
|
||||
FileCountLimitExceededException countLimit = null;
|
||||
for (Throwable cause = error;
|
||||
cause != null && causes.size() < 10;
|
||||
cause = cause.getCause()) {
|
||||
causes.add(cause.getClass().getName());
|
||||
if (cause instanceof FileCountLimitExceededException exceeded) countLimit = exceeded;
|
||||
}
|
||||
log.warn("multipart 上传被拒绝 method={} path={} contentLength={} maxParts={} errorTypes={}",
|
||||
request.getMethod(), request.getRequestURI(), request.getContentLengthLong(),
|
||||
countLimit == null ? null : countLimit.getLimit(), causes);
|
||||
if (countLimit != null)
|
||||
return business(new BusinessException(413, "UPLOAD_TOO_MANY_PARTS",
|
||||
"上传文件或表单项数量超过限制,请减少文件后重试"));
|
||||
return business(new BusinessException(413, "UPLOAD_TOO_LARGE", "上传超过大小限制"));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user