org.apache.tomcat.util.http.fileupload.FileUploadBase$FileSizeLimitExceededException: The field file exceeds its maximum permitted size of 2097152 bytes
上传文件时出错.它给我的api提供500错误,我应该处理这个错误并以JSON格式返回响应,而不是ErrorController中提供的错误
我想捕获该异常并给出JSON响应而不是ErrorPage.
@RequestMapping(value="/save",method=RequestMethod.POST)
public ResponseDTO@modelattribute @Valid FileUploadSingleDTO fileUploadSingleDTO,BindingResult bindingResult)throws MaxUploadSizeExceededException
{
ResponseDTO
接受文件的DTO如下
public class FileUploadSingleDTO {
@NotNull
private Integer documentName;
private Integer documentVersion;
@NotNull
private MultipartFile file;
}
最佳答案
我知道你可以通过使用它来处理多部分文件异常.
@ControllerAdvice
public class MyErrorController extends ResponseEntityExceptionHandler {
Logger logger = org.slf4j.LoggerFactory.getLogger(getClass());
@ExceptionHandler(MultipartException.class)
@ResponseBody
String handleFileException(HttpServletRequest request,Throwable ex) {
//return your json insted this string.
return "File upload error";
}
}