我想用 Vertx 发送文件部分图像,我想在像 Postman 这样的代码中复制这种情况
I want send a filepart image with Vertx, I want replicate this case in code like Postman
PostmanExample
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
目录中。
PostmanExample
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
目录中。