2017-06-23 140 views
1

我想从远程文件中取一个字节数组。我创建了AsyncStream,但不知道如何将其转换为适当的字节数组。用Finagle Http客户端下载文件

val client: Service[http.Request, http.Response] = 
    Http 
     .client 
     .withStreaming(enabled = true) 
     .newService("www.scala-lang.org:80") 

    val request = http.Request(http.Method.Get, "/docu/files/ScalaOverview.pdf") 
    request.host = "scala-lang.org" 
    val response: Future[http.Response] = client(request) 

    def fromReader(reader: Reader): AsyncStream[Buf] = 
    AsyncStream.fromFuture(reader.read(Int.MaxValue)).flatMap { 
     case None => AsyncStream.empty 
     case Some(a) => a +:: fromReader(reader) 
    } 

    val result: Array[Byte] = 
    Await.result(response.flatMap { 
     case resp => 
     fromReader(resp.reader) ??? // what to do? 
    }) 

回答

2

你不需要fromReaderAsyncStream已经拥有它。 所以,你可以做这样的事情:

val result: Future[Array[Byte]] = response 
    .flatMap { resp => 
    AsyncStream.fromReader(resp.reader) 
     .foldLeft(Buf.Empty){ _ concat _ } 
     .map(Buf.ByteArray.Owned.extract) 
    } 
1

使用scalaj下载文件。

import scalaj.http._ 

val response: HttpResponse[String] = Http("http://foo.com/search").param("q","monkeys").asString 

请参阅不同类型的请求的文档获取,邮政等

https://github.com/scalaj/scalaj-http

+0

感谢这个工具。 –