scalaj http-';执行';方法是“返回”;溪流已关闭”;

scalaj http-';执行';方法是“返回”;溪流已关闭”;,scala,stream,scalaj-http,Scala,Stream,Scalaj Http,我想使用scalaj http库从http连接下载大小为31gb的字节内容文件。”asBytes'不是选项,因为它重新运行字节数组 我试图使用“execute”方法返回输入流,但当我执行下面的程序时,它返回流已关闭。我想我没有把这条小溪读两遍 我做错了什么 val response: HttpResponse[InputStream] = Http(url).postForm(parameters).execute(inputStream => inputStream) if (

我想使用scalaj http库从http连接下载大小为31gb的字节内容文件。”asBytes'不是选项,因为它重新运行字节数组

我试图使用“execute”方法返回输入流,但当我执行下面的程序时,它返回流已关闭。我想我没有把这条小溪读两遍

我做错了什么

  val response: HttpResponse[InputStream] = Http(url).postForm(parameters).execute(inputStream => inputStream)

  if (response.isError) println(s"Sorry, error found: ${response.code}")
  else {
    val is: InputStream = response.body
    val buffer: Array[Byte] = Array.ofDim[Byte](1024)
    val fos = new FileOutputStream("xxx")
    var read: Int = 0

    while (read >= 0) {
      read = is.read(buffer)
      if (read > 0) {
        fos.write(buffer, 0, read)
      }
    }
    fos.close()
  }

无法导出
inputStream
,因为该流将在execute方法结束时关闭。 您应该在execute中使用流,如下所示:

  val response = Http(url).postForm(parameters).execute { is =>         
    val buffer: Array[Byte] = Array.ofDim[Byte](1024)
    val fos = new FileOutputStream("xxx")
    var read: Int = 0

    while (read >= 0) {
      read = is.read(buffer)
      if (read > 0) {
        fos.write(buffer, 0, read)
      }
    }
    fos.close()
  }

  if (response.isError) println(s"Sorry, error found: ${response.code}")