3

I built a Java servlet server with Maven and Spring, I am sending large files from an android device to the server. I have successfully sent files of size 6MB to 41MB but when I try send files of size 100MB or more I get the following error on the server:

org.apache.commons.fileupload.MultipartStream$MalformedStreamException: Stream ended unexpectedly 

Is this happening because the server is not able to handle request of this size? Is it possible to increase the size of allowed files? How?

Here is my server code:

try {
            List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);//Error happens here
            List<File> files = new ArrayList<File>();//Holds all the files that were received
            HashMap<String, String> map = new HashMap<String, String>();
            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();

                map.put(fieldName, fieldValue);
            } else {
                // Process form file field (input type="file").
                String fileName = FilenameUtils.getName(item.getName());//If the name was not specified set default

                String filePath = Constants.ORIGINAL_DIRECTORY_PATH + new SecureRandom().nextInt() + "_" +fileName;
                File storeFile = new File(filePath);
                item.write(storeFile);

                files.add(storeFile);
            }
        }
    } catch (FileUploadException e) {
        throw new ServletException("Cannot parse multipart request.", e);
    }
develop1
  • 747
  • 1
  • 10
  • 32

2 Answers2

1

From the message it seems that the connection is closed by either client or server or some proxy in the middle.

Connections are usually closed when timeout occurs.

While normally I would not expect timeout to occur during the file upload, it still can occur in case your server is having memory problems and GC is working hard. So I suggest to monitor the GC times during the file upload. If the are high, you are having memory problem and should not store the whole content of file in memory.

Tarlog
  • 10,024
  • 2
  • 43
  • 67
1

Most likely has to do with a timeout. Sometimes if a servlet is loading a request for too long and the server has a limit it will stop. This happens on Google App engine where the limit is 60 seconds. The way around it was to create a task queue so the processing is not dependent on the servlet.

Jonathan Laliberte
  • 2,672
  • 4
  • 19
  • 44