如何在Scala中使用Play WS 2.5停止流式下载?
我想下载图像的前几kb,然后从内容中获取InputStream。最后,我使用如何在Scala中使用Play WS 2.5停止流式下载?,scala,playframework,akka-stream,Scala,Playframework,Akka Stream,我想下载图像的前几kb,然后从内容中获取InputStream。最后,我使用ImageIO获取图像的尺寸,它只需要读取一点点 我的代码可以快速从流中读取图像,并在读取少量图像后返回,但下载会在后台继续进行,这会占用传输带宽 ws.url(uri).withMethod("GET").stream().map { res => val (killSwitch, is) = res.body .viaMat(KillSwitches.single)(Keep.right)
ImageIO
获取图像的尺寸,它只需要读取一点点
我的代码可以快速从流中读取图像,并在读取少量图像后返回,但下载会在后台继续进行,这会占用传输带宽
ws.url(uri).withMethod("GET").stream().map { res =>
val (killSwitch, is) = res.body
.viaMat(KillSwitches.single)(Keep.right)
.toMat(StreamConverters.asInputStream())(Keep.both)
.run()
val dimensions = dimensionsFromImageIo(is)
is.close()
// This doesn't stop the download either
killSwitch.shutdown()
}
我是Akka Streams的新手,不知道如何从播放中的流媒体文档中获取一些信息,以便能够消费一点,像在InputStream中一样获取它,并停止下载
ws.url(uri).withMethod("GET").stream().map { res =>
val is = res.body.runWith(StreamConverters.asInputStream())
// Efficiently gets dimensions without needing the entire file
val dimensions = dimensionsFromImageIo(is)
// This doesn't stop the download :(
is.close()
}
然后,我尝试了类似的方法,但在killSwitch
上调用shutdown
或abort
都没有停止下载,这将在后台继续,我可以通过监视带宽看到
ws.url(uri).withMethod("GET").stream().map { res =>
val (killSwitch, is) = res.body
.viaMat(KillSwitches.single)(Keep.right)
.toMat(StreamConverters.asInputStream())(Keep.both)
.run()
val dimensions = dimensionsFromImageIo(is)
is.close()
// This doesn't stop the download either
killSwitch.shutdown()
}