I'm experiencing strange behaviour when using Apache FileUpload with filesize limit and Tomcat web application server (version 9). Here's my code:
List<FileItem> multi = null;
try {
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setRepository(new File(System.getProperty("java.io.tmpdir")));
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setFileSizeMax(2 * 1024 * 1024); // 2MB
upload.setHeaderEncoding("UTF-8");
multi = upload.parseRequest(request);
...
} catch (Exception e) {
e.printStackTrace();
}
// send servlet response
...
As visible above, the file size limit is 2MB.
If I upload a 1MB file, everything is OK, my servlet response is sent to client.
If I upload a 3MB file, behaviour is OK: the exception is thrown and my servlet response is sent to client.
If I upload a 10MB file, behaviour is NOT OK. The exception is thrown but my servlet response is never sent to client. WireShark shows me that tomcat respond with RST message and then client tries to send the request again and again.
After long debug, I could see that I could bypass the reset code (RST) by setting the maxSwallowSize
connector attribute (defined in server.xml
) to -1, or to a value bigger that 10MB.
Everything is OK if I remove setFileSizeMax()
call.
My questions are:
- Why is behaviour different on 3MB and 10MB files since they are both larger than 2BM limit ?
- What is role of
maxSwallowSize
when file is smaller/larger than limit