Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Scala中使用Play WS 2.5停止流式下载?_Scala_Playframework_Akka Stream - Fatal编程技术网

如何在Scala中使用Play WS 2.5停止流式下载?

如何在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)

我想下载图像的前几kb,然后从内容中获取InputStream。最后,我使用
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()
}