Apache commons fileupload timeout only with Firefox
Asked Answered
M

4

8

I use the Apache commons fileupload 1.4 library in my java project. I have a html part with a classic form with a file input and some hidden fields.

I have a problem with uploading files of around >500ko only with Firefox >= 52

It works well with files of 10mo in Chrome or Internet Explorer. But with Firefox, I have a timeout after waiting several minutes after submitting the form.

After some debugging, I see that the code responsible of the timeout is :

List<FileItem> items = (new ServletFileUpload(new DiskFileItemFactory())).parseRequest(request);

The part with cause wait is "parseRequest".

I try to debug the content of request with debugger in IntelliJ, but there is no way to copy entire content value of this request object in raw format.

It's working in these cases : - Firefox : version <= 52 or file size < 500ko (around, it's not really precise) - Internet Explorer - Chrome

There is no file size limit, it seems that depends on the request size, because the parsing request part is taking too much time...

I get the HTTP request with a Firefox extension in two cases. One generating uploading a file of 3mo which doesn't works (the request file is huge, 3x the size of the uploaded file) : https://code.empreintesduweb.com/13561.html

One generated uploading a file of 200ko which works (the request file is small) : https://code.empreintesduweb.com/13560.html

In fact, the main difference is that in Chrome or IE, I don't have the raw content of the uploaded file in the request headers :

The part with : obj stream .... endstream endobj

Only appear with Firefox...

Meyers answered 10/5, 2019 at 11:44 Comment(0)
M
0

Thanks for all your answer. Finally, I successfully resolve this issue, but in fact... not really. I notice that there was some specific things in my form. I had two inputs, one standard file input, and another which receive the file content encoded in base64 by some weird js before any upload. So I was having one time the raw content of the file, and also the file in base64. Why ?! I don’t know.

But I delete all this, I create a new simple and clean form with a standard input file. I use the stream API from ServletFileUpload, and it works, and takes only few seconds for big files.

So I don’t understand everything (why the problem was only on some browser for example), but I find a solution ;)

Thank you !

Meyers answered 4/6, 2019 at 21:6 Comment(0)
F
2

A few things that are worth to try here:

We may also help you better if you provide some more informations, like the versions of apache / java / servlet, and a few more code (especially the definition of request)

Some ressources that could be helpful:
XMLHttpRequest
Sending_files_using_a_FormData_object
How to set a header for a HTTP GET request, and trigger file download?

Floatable answered 14/5, 2019 at 9:17 Comment(0)
W
2

You can try setting the maximum file size, maybe the file size exceeds the maximum threshold .According to the documentation :

  • Uploaded items should be retained in memory as long as they are reasonably small.
  • Larger items should be written to a temporary file on disk.
  • Very large upload requests should not be permitted.
  • The built-in defaults for the maximum size of an item to be retained in memory, the maximum permitted size of an upload request, and the location of temporary files are acceptable.

Try the following :

 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            // Set factory constraints
           DiskFileItemFactory factory = new DiskFileItemFactory();
           factory.setSizeThreshold(yourMaxMemorySize);
           ServletContext servletContext = this.getServletConfig().getServletContext();
           File repository = (File) servletContext.getAttribute("javax.servlet.context.tempdir");
           factory.setRepository(repository);
            List<FileItem> items = new ServletFileUpload(factory).parseRequest(request);
            for (FileItem item : items) {
                if (item.isFormField()) {
                    // Process regular form field (input type="text|radio|checkbox|etc", select, etc).
                    String fieldName = item.getFieldName();
                    String fieldValue = item.getString();
                    // ... (do your job here)
                } else {
                    // Process form file field (input type="file").
                    String fieldName = item.getFieldName();
                    String fileName = FilenameUtils.getName(item.getName());
                    InputStream fileContent = item.getInputStream();
                    // ... (do your job here)
                }
            }
        } catch (FileUploadException e) {
            throw new ServletException("Cannot parse multipart request.", e);
        }

        // ...
    }

Here, we are providing a temp location for the file since the file is large.

Wonderful answered 14/5, 2019 at 16:24 Comment(0)
D
1

try this to set session timeout using setMaxInactiveInterval method

 request.getSession().setMaxInactiveInterval(1200);

parameter Specifies the time, in seconds, between client requests before the servlet container will invalidate this session. An interval value of zero or less indicates that thesession should never timeout.

Dishonorable answered 18/5, 2019 at 12:29 Comment(1)
It’s just gonna extend the time to wait but not explain €6 this is so long.Meyers
M
0

Thanks for all your answer. Finally, I successfully resolve this issue, but in fact... not really. I notice that there was some specific things in my form. I had two inputs, one standard file input, and another which receive the file content encoded in base64 by some weird js before any upload. So I was having one time the raw content of the file, and also the file in base64. Why ?! I don’t know.

But I delete all this, I create a new simple and clean form with a standard input file. I use the stream API from ServletFileUpload, and it works, and takes only few seconds for big files.

So I don’t understand everything (why the problem was only on some browser for example), but I find a solution ;)

Thank you !

Meyers answered 4/6, 2019 at 21:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.