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
.