-1

I have a file CRUD rest API where clients can upload files. These files can be as big as 1 GB.

What I want to do is to run some validation on other things (and possibly filename) before starting the upload. But Jersey's FormDataMultiPart waits for full upload before starting processing the request handler.

Maybe it's how HTTP works but if not, I want Jersey to hold the upload until I start reading from InputStream, saving a whole lot of effort and bandwidth. Is that possible?

Current code:

@PUT
@Consumes(MediaType.MULTIPART_FORM_DATA)
public void uploadFile(@NotNull FormDataMultiPart formDataMultiPart) {
    validateOtherThings(); // throws validation errors
    FormDataBodyPart formDataBodyPart = formDataMultiPart.getField("file");
    String filename = formDataBodyPart.getContentDisposition().getFileName();
    validateFilename(filename);
    InputStream inputStream = formDataBodyPart.getValueAs(InputStream.class);
    saveFile(filename, inputStream);
}

I have tried:

@PUT
@Consumes(MediaType.MULTIPART_FORM_DATA)
public void uploadFile(@Context HttpServletRequest request) {
    ...
    InputStream inputStream = request.getPart("file").getInputStream();
    ...
}

This actually goes into the method immediately but fails when doing getPart("file") with java.lang.IllegalStateException: No multipart config for servlet.

Natan
  • 2,816
  • 20
  • 37

1 Answers1

-1

I don't know if it is possible. Look at sample POST request with files: https://stackoverflow.com/a/23517227/158037 - Jersey needs to read whole request to know which fields are present before creating FormDataMultiPart object. I guess it would be possible using Servlet (doPut()) and manually parse incoming data with something like https://github.com/synchronoss/nio-multipart but I doubt it is worth it. Just do the validation on client side, before sending and on server side, after receiving whole file.

user158037
  • 2,659
  • 1
  • 24
  • 27