fun sendFileToMatch(path:String){
val client = WebClient.create(vertx);
var form = MultipartForm.create()
.binaryFileUpload("image","imageName" , path, "image/jpeg")
client.post(8888, "localhost", "/search?")
.putHeader("content-type", "multipart/form-data")
.sendMultipartForm(form) { }
}
当我运行代码显示错误的请求时,我已经完全输入了关键的“图像”并发送了文件部分图像
TL; DR-您的客户代码看起来不错。
唯一可疑的部分是它path
本身,因为您没有指定如何在代码中准确地获取它,也没有指定您如何处理服务器响应的事实:您只需{}
在示例中进行操作
不过,这是一个完整的示例供您参考:
val vertx = Vertx.vertx()
val router = Router.router(vertx)
router.route().handler(BodyHandler.create());
router.post("/search").handler {
val uploads: Set<FileUpload> = it.fileUploads()
uploads.forEach { upload ->
println(upload.name()) // "image"
println(upload.fileName()) // "imageName"
println(upload.size()) // 42537
}
it.response().end("OK!")
}
vertx.createHttpServer().requestHandler(router)
.listen(8888)
// We read the PNG file from /resources
val path = object {}.javaClass.getResource("5EWx9.png").path
val form = MultipartForm.create()
.binaryFileUpload("image","imageName" , path, "image/png")
val client = WebClient.create(vertx);
client.post(8888, "localhost", "/search?")
.putHeader("content-type", "multipart/form-data")
.sendMultipartForm(form) {
if (it.succeeded()) {
println(it.result().bodyAsString()) // "OK!"
}
else {
println(it.cause())
}
}
作为要上传的文件,我使用了您提供的PostmanExample,它是一个PNG图像,放在/resources
我的项目目录中。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句