文件上传中,可以限定文件大小,防止用户上传过大的文件,但是出现异常会报错,不够优雅

这里做异常捕获,然后自定义提示文字,可以更加优雅的解决文件上传超出限制报异常
- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
/**
* 上传文件超出大小限制异常 适用于springboot 2.x
* @param ex
* @return
*/
public AjaxResult handleBusinessException(MaxUploadSizeExceededException ex) {
String msg;
Throwable rootCase=ex.getRootCause();
if (rootCase instanceof org.apache.tomcat.util.http.fileupload.impl.FileSizeLimitExceededException) {
System.out.println(ex.getMessage()+"上传文件过大[单文件大小不得超过50M]");
msg = "上传文件过大[单文件大小不得超过50M]";
} else if (rootCase instanceof org.apache.tomcat.util.http.fileupload.impl.SizeLimitExceededException) {
System.out.println(ex.getMessage()+"上传文件过大[总上传文件大小不得超过50M]");
msg = "上传文件过大[总上传文件大小不得超过50M]";
} else {
msg = "上传文件失败";
}
return AjaxResult.error(msg);
}
报异常的时候就更加的优雅了
