加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

多部分文件上载:弹出引导返回JSON错误消息中的大小超过异常

发布时间:2020-12-15 01:25:06 所属栏目:大数据 来源:网络整理
导读:因为我设置了最大文件上传限制,所以我得到了 org.apache.tomcat.util.http.fileupload.FileUploadBase$FileSizeLimitExceededException: The field file exceeds its maximum permitted size of 2097152 bytes 上传文件时出错.它给我的api提供500错误,我应该

因为我设置了最大文件上传限制,所以我得到了

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

接受文件的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";
  }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读