Kotlin KTOR-在POST路由中解压缩文件
我想解压在Ktor(bloc rounting)中http查询(内容类型:application/x-gzip)主体中发送的zip文件。 我试过这个: val zip_received=call.receiveStream() val incomingContent=gzip输入流(zip_已接收).toByteReadChannel() 但我有一个错误: java.lang.IllegalStateException:不允许在此调度程序上获取阻塞原语。考虑使用异步通道或使用上下文(DealpHist.IO){List.Access()来代替。{{}}。< /P> 我不会写这样的函数。 能给我一些帮助吗Kotlin KTOR-在POST路由中解压缩文件,kotlin,ktor,Kotlin,Ktor,我想解压在Ktor(bloc rounting)中http查询(内容类型:application/x-gzip)主体中发送的zip文件。 我试过这个: val zip_received=call.receiveStream() val incomingContent=gzip输入流(zip_已接收).toByteReadChannel() 但我有一个错误: java.lang.IllegalStateException:不允许在此调度程序上获取阻塞原语。考虑使用异步通道或使用上下文(DealpH
谢谢您可以使用以下代码将GZip未压缩请求正文作为字符串读取:
import io.ktor.server.engine.*
import io.ktor.server.netty.*
import io.ktor.application.*
import io.ktor.request.*
import io.ktor.routing.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import java.io.InputStream
import java.util.zip.GZIPInputStream
fun main(args: Array<String>) {
embeddedServer(Netty, port = 9090) {
routing {
post("/") {
withContext(Dispatchers.IO) {
call.receive<InputStream>().use { stream ->
val gzipStream = GZIPInputStream(stream)
val uncompressedBody = String(gzipStream.readAllBytes())
println(uncompressedBody)
}
}
}
}
}.start()
}
导入io.ktor.server.engine*
导入io.ktor.server.netty*
导入io.ktor.application*
导入io.ktor.request*
导入io.ktor.routing*
导入kotlinx.coroutines.Dispatchers
导入kotlinx.coroutines.withContext
导入java.io.InputStream
导入java.util.zip.gzip输入流
趣味主线(args:Array){
嵌入式服务器(网络,端口=9090){
路由{
职位(“/”){
withContext(Dispatchers.IO){
call.receive()。使用{stream->
val gzipStream=GZIPInputStream(流)
val uncompressedBody=String(gzipStream.readAllBytes())
println(解压体)
}
}
}
}
}.start()
}
您好。谢谢。它工作得很好。我只需要更改readBytes()而不是readAllBytes,因为此方法不存在。可能是我的GZIPInputStream版本。