javauploadjettyrestlet

Unable to upload file larger than 10MB?


Here is my code for uploading file:

@Override
public Representation post(Representation entity) {
    try {
        if (entity != null) {
            if (MediaType.MULTIPART_FORM_DATA.equals(entity.getMediaType(), true)) {
                DiskFileItemFactory factory = new DiskFileItemFactory();
                factory.setSizeThreshold(100000000);
                RestletFileUpload upload = new RestletFileUpload(factory);
                FileItemIterator fileIterator = upload.getItemIterator(entity);
                while (fileIterator.hasNext()) {
                    FileItemStream item = fileIterator.next();
                    String name = item.getName();
                    byte[] byteContent  = ByteStreams.toByteArray(item.openStream());
                    if(byteContent != null) {
                        String result = doSomethingWith(byteContent);
                        Representation response = new StringRepresentation(result);
                        response.setMediaType(MediaType.APPLICATION_JSON);
                        return response;
                    }
                }
            } else {
                return badRequest();
            }
        } else {
            return badRequest();
        }
    } catch (Exception e) {
        return internalError();
    }
    return null;
}

When deployed in Jetty (Java 8) it works when the file size is approximately lower then 10MB, small files like 1-2 MB, when the file is bigger like 10MB+ the upload fails randomly, what could be wrong with Restet or Jetty?


Solution

  • The fixed for this issue, was to update the max payload size of the NGINX server in front of Jetty.