上传大于允许限制的文件时出现FileSizeLimitExceededException

时间:2017-10-18 16:06:31

标签: java exception-handling jetty multipartform-data apache-commons-fileupload

public static void parseUpload(HttpServletRequest request) throws AttachmentException
{
    if (ServletFileUpload.isMultipartContent(request))
{
    DiskFileItemFactory factory = new DiskFileItemFactory();
    factory.setSizeThreshold(10 * 1024 * 1024); // 10 MB
    ServletFileUpload uploader = new ServletFileUpload(factory);

    uploader.setSizeMax(3 * 1024 * 1024); // allowed file size
    uploader.setFileSizeMax(3 * 1024 * 1024);

    List<FileItem> items;
    try
    {
        items = uploader.parseRequest(request); // throws exception
        for (FileItem fileItem : items)
        {

        }
    }
    catch (FileUploadBase.FileSizeLimitExceededException e)
    {
        // catch block 1 (stack trace printed)
        throw new AttachmentException(e); // This is being caught in JSP
    }
    catch (FileUploadException e)
    {
        // catch block 2
    }
    catch (Exception e)
    {
        // catch block 3
    }
}
}

我从JSP调用此函数,当我上传大小小于3 MB的文件时,工作正常。

  

当我上传大小超过3 MB的文件时,catch块会捕获异常但Web服务器返回502.应用程序部署在JETTY上。

应用程序暂停一会儿,延迟量与附件大小成正比。

异常追踪:

parseUpload <@> FileSizeLimitExceededException with error message : The field urlname exceeds its maximum permitted size of 3145728 bytes. 
org.apache.commons.fileupload.FileUploadBase$FileItemIteratorImpl$FileItemStreamImpl$1.raiseError(FileUploadBase.java:789)
org.apache.commons.fileupload.util.LimitedInputStream.checkLimit(LimitedInputStream.java:78) 
org.apache.commons.fileupload.util.LimitedInputStream.read(LimitedInputStream.java:137)
java.io.FilterInputStream.read(FilterInputStream.java:107) 
org.apache.commons.fileupload.util.Streams.copy(Streams.java:100)
org.apache.commons.fileupload.util.Streams.copy(Streams.java:70)
org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:347) 
org.apache.commons.fileupload.servlet.ServletFileUpload.parseRequest(ServletFileUpload.java:115)
parseUpload(UploadUtils.java:255)
  

似乎无论如何捕获到异常,文件仍然会被解析或者再次抛出了一些其他异常。

我已经浏览了类似的帖子,但没有提供确切原因以及如何处理此案例。请在这里提供一些帮助。

0 个答案:

没有答案