如何使用 HttpRequest.sendStream() 获取上传文件的状态?

How to get status of uploading file using HttpRequest.sendStream()?

我写了一个将文件上传到云服务的函数。效果不错,就是不知道怎么查看上传进度

suspend fun uploadFile(path: Path) {
    ...
    val asyncFile: AsyncFile = awaitResult { fs.open(path.toString(), OpenOptions(), it) }
    val methodType: HttpMethod = when (link.method.toLowerCase()) {
        "put" -> HttpMethod.PUT
        "post" -> HttpMethod.POST
        else -> throw UnsupportedOperationException("Method is not supported")
    }
    val request: HttpRequest<Buffer> = webClient.requestAbs(methodType, link.href)
    val response: HttpResponse<Buffer> = awaitResult { request.sendStream(asyncFile, it) }
}

您可以使用委托包装 AsyncFile 提供的 ReadStream 接口,并在 handler 方法中使用 AsyncFile 发出的字节数更新计数器这会告诉您已发送了多少文件。