HowTo: Avoid saving multipart form upload files into the upload directory in Vert.x
Recently, I have used Vert.x to handle multipart form file upload.
Default implementation of handling file upload is BodyHandler which saves the uploaded files to the upload directory, and in the next pipeline handler, the uploaded files can be read from the upload directory:
router.post(uri).handler(BodyHandler.create().setMergeFormAttributes(true));
router.post(uri).handler(someHandler);
But I think, it could come to an performance issue, even if some comments about this can be found in vert.x community.
In our case, our mobile apps will send a lot of requests with file upload which size is max. 100KB in multipart form. Our http vert.x collector has to handle a lot of requests, it has to retrieve the byte array of upload file from multipart form very fast and forward it to Kafka.
That is why I had to implement another BodyHandler which does not save uploaded files to the upload directory, but put the byte array of it to memory, namely routing context.
My custom MultipartBodyHandler can be found in git repo:
Let’s see the following code listing using MultipartBodyHandler
:
router.post(uri).handler(MultipartBodyHandler.create().setMergeFormAttributes(true));
router.post(uri).handler(someHandler);
You can just define MultipartBodyHandler instead of BodyHandler with little difference.
Next, I will show you how to get byte array of upload file. someHandler
could have such listing:
// multipart form field: file upload section.
Set<FileUpload> fileUploadSet = routingContext.fileUploads();
for(FileUpload fileUpload : fileUploadSet) {
// file upload field name of form.
String fieldName = fileUpload.name();
// upload file data.
byte[] uploadData = ((MultipartFileUploadImpl)fileUpload).getData(); // content type.
String dataContentType = fileUpload.contentType();
}
// multipart form field: text data section.
MultiMap formAttributes = request.formAttributes();
As seen in the listing, you can get byte array of uploaded file from routingContext
. You can also get another text data from multipart form.
With this approach, the uploaded files don’t have to be saved into the upload direcotry, instead, it will be saved in memory, and our vert.x http server can handle a lot of our multipart requests without performance issue.