我想上传头像。我的端点是使用 OpenAPI3RouterFactory(api.yaml 文件)设置的。
api.yaml:
#other endpoints
/api/v1/upload-avatar:
post:
summary: Uploading avatars endpoint
operationId: upload-avatar
tags:
- sign up
- registration
#other ones
HandlerVerticle.kt:
//OpenApi3Router is set
//other endpoints
routerFactory.addHandlerByOperationId("upload-avatar", BodyHandler.create().setDeleteUploadedFilesOnEnd(true).setUploadsDirectory("mp-upload").setMergeFormAttributes(true))
routerFactory.addHandlerByOperationId("upload-avatar", { routingContext ->
val fileUploadSet = routingContext.fileUploads()
val fileUploadIterator = fileUploadSet.iterator()
while (fileUploadIterator.hasNext()) {
val fileUpload = fileUploadIterator.next()
val uploadedFile = vertx.fileSystem().readFileBlocking(fileUpload.uploadedFileName())
try {
val fileName = URLDecoder.decode(fileUpload.fileName(), "UTF-8")
vertx.fileSystem().writeFileBlocking(fileName , uploadedFile)
routingContext.response().end()
} catch (e: Exception) {
e.printStackTrace()
}
}
})
//other routes
当我上传图片时,它上传成功,但在服务器端 vertx 创建了错误的上传目录,并且它不会删除临时上传的文件。谁能帮我?