0

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?

quarks
  • 33,478
  • 73
  • 290
  • 513

2 Answers2

0

Set an attribute on the Server instance to modify maximum content size. Jetty limits the amount of data to stop DOS attacks. You can change the form upload size permission with configuration as below. Link to documentation

<configure class="org.eclipse.jetty.server.Server">
      <Call name="setAttribute">
      <Arg>org.eclipse.jetty.server.Request.maxFormContentSize</Arg>
      <Arg>200000</Arg>
    </Call>
</configure>
Steps
  • 481
  • 5
  • 9
bpjoshi
  • 1,091
  • 16
  • 23
0

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

quarks
  • 33,478
  • 73
  • 290
  • 513