I find another way to decode the multipart data in real time. With the help of netty decoder. see the snippet code below:
// the netty decoder used to decode multipart data in real time
HttpPostMultipartRequestDecoder decoder = null;
// create the decoder with reflect method
private void createDecoder(HttpServerRequest wrapper) {
try {
Class<?> wrapperClass = Class.forName("io.vertx.ext.web.impl.HttpServerRequestWrapper");
Field delegateField = wrapperClass.getDeclaredField("delegate");
delegateField.setAccessible(true);
HttpServerRequestImpl request = (HttpServerRequestImpl) delegateField.get(wrapper);
Field requestField = request.getClass().getDeclaredField("request");
requestField.setAccessible(true);
HttpRequest httpRequest = (HttpRequest) requestField.get(request);
decoder = new HttpPostMultipartRequestDecoder(new DefaultHttpDataFactory(DefaultHttpDataFactory.MINSIZE),
httpRequest);
} catch (ClassNotFoundException | NoSuchFieldException | SecurityException | IllegalArgumentException
| IllegalAccessException ex) {
log.error("create decoder failed {}.", ex.getMessage());
}
log.info("create decoder finished. {}", decoder);
}
router.route().handler(BodyHandler.create());
router.post("/some/path/uploads").handler(routingContext -> {
// NOTE: in vertx, request is a class of 'HttpServerRequestWrapper'
HttpServerRequest requestWrapper = routingContext.request();
createDecoder(requestWrapper);
// decode the buffer in real time
request.handler(buffer -> {
decoder.offer(new DefaultHttpContent(buffer.getByteBuf()));
if (decoder.hasNext()) {
// get the attributes and other data
// ...
}
});
});