微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

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";
  }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐