|
|
|
|
@ -28,9 +28,12 @@ import org.springframework.web.bind.MissingServletRequestParameterException;
|
|
|
|
|
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 org.springframework.web.servlet.NoHandlerFoundException;
|
|
|
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
import javax.validation.ConstraintViolation;
|
|
|
|
|
import javax.validation.ConstraintViolationException;
|
|
|
|
|
import javax.validation.ValidationException;
|
|
|
|
|
@ -385,4 +388,90 @@ public class GlobalExceptionHandler {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 处理文件上传大小超出限制异常
|
|
|
|
|
*/
|
|
|
|
|
@ExceptionHandler(MaxUploadSizeExceededException.class)
|
|
|
|
|
public CommonResult<String> handleMaxUploadSizeExceededException(
|
|
|
|
|
MaxUploadSizeExceededException e,
|
|
|
|
|
HttpServletRequest request,
|
|
|
|
|
HttpServletResponse response) {
|
|
|
|
|
|
|
|
|
|
log.error("文件上传大小超出限制: {}", request.getRequestURI(), e);
|
|
|
|
|
|
|
|
|
|
// 从异常信息中提取实际大小和限制大小
|
|
|
|
|
String errorMessage = extractSizeInfo(e);
|
|
|
|
|
|
|
|
|
|
return CommonResult.error(400,
|
|
|
|
|
String.format("文件大小超出限制。%s 请上传小于500MB的文件", errorMessage)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 处理 Multipart 异常
|
|
|
|
|
*/
|
|
|
|
|
@ExceptionHandler(MultipartException.class)
|
|
|
|
|
public CommonResult<String> handleMultipartException(
|
|
|
|
|
MultipartException e,
|
|
|
|
|
HttpServletRequest request) {
|
|
|
|
|
|
|
|
|
|
log.error("文件上传异常: {}", request.getRequestURI(), e);
|
|
|
|
|
|
|
|
|
|
if (e.getMessage().contains("SizeLimitExceededException")) {
|
|
|
|
|
return CommonResult.error(400, "文件大小超出限制,请上传小于500MB的文件");
|
|
|
|
|
} else if (e.getMessage().contains("FileSizeLimitExceededException")) {
|
|
|
|
|
return CommonResult.error(400, "单个文件大小超出限制,请上传小于500MB的文件");
|
|
|
|
|
} else if (e.getMessage().contains("临时文件夹")) {
|
|
|
|
|
return CommonResult.error(400, "临时文件夹不可用,请检查磁盘空间");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return CommonResult.error(400, "文件上传失败:" + e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 从异常信息中提取大小信息
|
|
|
|
|
*/
|
|
|
|
|
private String extractSizeInfo(MaxUploadSizeExceededException e) {
|
|
|
|
|
String message = e.getMessage();
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// 解析异常信息,提取大小数据
|
|
|
|
|
if (message.contains("exceeds the configured maximum")) {
|
|
|
|
|
// 格式: ... size (367254613) exceeds the configured maximum (104857600)
|
|
|
|
|
String sizePart = message.substring(message.indexOf("size (") + 6);
|
|
|
|
|
String actualSizeStr = sizePart.substring(0, sizePart.indexOf(")"));
|
|
|
|
|
|
|
|
|
|
String maxPart = message.substring(message.indexOf("maximum (") + 9);
|
|
|
|
|
String maxSizeStr = maxPart.substring(0, maxPart.indexOf(")"));
|
|
|
|
|
|
|
|
|
|
long actualSize = Long.parseLong(actualSizeStr);
|
|
|
|
|
long maxSize = Long.parseLong(maxSizeStr);
|
|
|
|
|
|
|
|
|
|
return String.format("实际大小: %s, 限制大小: %s",
|
|
|
|
|
formatFileSize(actualSize),
|
|
|
|
|
formatFileSize(maxSize)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception ex) {
|
|
|
|
|
log.error("解析文件大小信息失败", ex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return "请检查文件大小";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 格式化文件大小
|
|
|
|
|
*/
|
|
|
|
|
private String formatFileSize(long size) {
|
|
|
|
|
if (size < 1024) {
|
|
|
|
|
return size + " B";
|
|
|
|
|
} else if (size < 1024 * 1024) {
|
|
|
|
|
return String.format("%.2f KB", size / 1024.0);
|
|
|
|
|
} else if (size < 1024 * 1024 * 1024) {
|
|
|
|
|
return String.format("%.2f MB", size / (1024.0 * 1024.0));
|
|
|
|
|
} else {
|
|
|
|
|
return String.format("%.2f GB", size / (1024.0 * 1024.0 * 1024.0));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|