vertx如何接收-data-binary形式上传的文件
使用如下的方式上传文件给某个服务:
curl -H 'X-SWR-Override:1' https://ip:port/repo/v2/file_paths/kafka.tar.gz --data-binary @kafka.tar.gz -X PUT
服务端用vertx应该如何实现?
下面是搜了资料说可以多次调用下面的语句,但是具体怎么实现呢?
这是因为body可能会非常大 (例如一个文件上传),我们通常不不会吧缓冲的整个body放在内
存中交给你,因为那将导致服务器内存用尽。
若要接收body,您可以使用handler请求,调用后,会有请求body到达。
request.handler(buffer -> {
System.out.println("I have received a chunk of the body of length " + buffer.length(
));
});
传递到handler的对象是一个Buffer,该handler可以调用多次,当数据到达时从网络,根据
body的大小。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Handling file uploads
Body handler is also used to handle multi-part file uploads.
If a body handler is on a matching route for the request, any file uploads will be automatically streamed to the uploads directory, which is file-uploads by default.
Each file will be given an automatically generated file name, and the file uploads will be available on the routing context with fileUploads.
Here’s an example:
router.route().handler(BodyHandler.create());
router.post("/some/path/uploads").handler(routingContext -> {
Set<FileUpload> uploads = routingContext.fileUploads();
// Do something with uploads....
});
Each file upload is described by a FileUpload instance, which allows various properties such as the name, file-name and size to be accessed.