问题
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
}
}
}
I am calling this function from a JSP and things work just fine when I'm uploading a file with size less than 3 MBs.
The moment I upload a file with size greater than 3 MBs the catch block catches the exception but web server returns 502. The application is deployed on JETTY.
The application hangs for a moment and the amount of delay is proportional to the size of attachment.
Exception trace:
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)
It seems that however the exception is caught the file is still getting parsed or some other exception has been thrown again.
I have gone through similar posts but none provides the exact reason for this and how to handle this case. Please provide some help here.
来源:https://stackoverflow.com/questions/46814571/filesizelimitexceededexception-while-uploading-file-greater-than-allowed-limit