SpringBoot 当文件上传大小限制超过获取 MultipartException 而不是 MaxUploadSizeExceededException [英] SpringBoot When file upload size limit exceeds getting MultipartException instead of MaxUploadSizeExceededException

查看:34
本文介绍了SpringBoot 当文件上传大小限制超过获取 MultipartException 而不是 MaxUploadSizeExceededException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有简单的 SpringBoot 应用文件上传功能,其中最大文件上传文件大小为 2 MB.

I have simple SpringBoot app file uploading functionality where max file upload file size is 2 MB.

我已经配置了 multipart.max-file-size=2MB 它工作正常.但是当我尝试上传大于 2 MB 的文件时,我想处理该错误并显示错误消息.

I have configured multipart.max-file-size=2MB it is working fine. But when I try to upload files with larger than 2 MB size I want to handle that error and show the error message.

为此,我让我的控制器实现了 HandlerExceptionResolverresolveException() 实现如下:

For that I have my controller implements HandlerExceptionResolver with resolveException() implementation as follows:

public ModelAndView resolveException(HttpServletRequest request,
            HttpServletResponse response, Object handler, Exception exception)
    {        
        Map<String, Object> model = new HashMap<String, Object>();
        if (exception instanceof MaxUploadSizeExceededException)
        {
            model.put("msg", exception.getMessage());
        } else
        {
            model.put("msg", "Unexpected error: " + exception.getMessage());
        }

        return new ModelAndView("homepage", model);
    }

问题是我得到的异常是 MultipartException 而不是 MaxUploadSizeExceededException.

The problem is the Exception Im getting is MultipartException instead of MaxUploadSizeExceededException.

堆栈跟踪是:无法解析多部分 servlet 请求;嵌套异常是 java.lang.IllegalStateException: org.apache.tomcat.util.http.fileupload.FileUploadBase$FileSizeLimitExceededException: 字段 myFile 超出其最大允许大小 2097152 字节.

在文件大小超过的情况下,为什么我没有收到 MaxUploadSizeExceededException?我得到了它的父异常 MultipartException,除了文件大小超出之外,还有许多其他原因可能会发生这种情况.

In the case of file size exceeds why not I am getting MaxUploadSizeExceededException? I am getting its parent Exception MultipartException which can be occured for many other reasons in addition to File Size exceeds.

对此有什么想法吗?

推荐答案

我遇到了同样的问题,看起来只有 Commons File Upload 的 MultipartResolver 实现抛出 MaxUploadSizeExceededException 而不是 MultipartResolver Servlet 3.0 实现.

I faced the same issue, it looks like only the MultipartResolver of Commons File Upload implementation throws MaxUploadSizeExceededException but not the MultipartResolver Servlet 3.0 implementation.

这是我到目前为止所做的.这里的关键是允许在控制器上检查文件,然后您可以验证大小并设置错误.

Here's what I have done so far. The key here was to allow the file to be check on the controller, then you can validate size and set an error.

  1. 在下面设置多部分属性多部分:最大文件大小:-1最大请求大小:-1

  1. set multipart properties below multipart: max-file-size: -1 max-request-size: -1

设置 Tomcat 8 (maxSwallowSize=-1")

set Tomcat 8 (maxSwallowSize="-1")

在控制器上,添加检查大小的逻辑

on controller, add logic to check size

if(fileAttachment.getSize() > 10485760 ) {抛出新的 MaxUploadSizeExceededException(fileAttachment.getSize());}

if(fileAttachment.getSize() > 10485760 ) { throw new MaxUploadSizeExceededException(fileAttachment.getSize()); }

这篇关于SpringBoot 当文件上传大小限制超过获取 MultipartException 而不是 MaxUploadSizeExceededException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆