【问题标题】:Play file upload : warn: Sending an 2xx 'early' response before end of request was received播放文件上传:警告:在收到请求结束之前发送 2xx“早期”响应
【发布时间】:2018-01-25 17:11:00
【问题描述】:

尝试“播放 Scala 文件上传示例”,我收到以下警告:

[warn] a.a.ActorSystemImpl - Sending an 2xx 'early' response before end of request was received... Note that the connection will be closed after this response. Also, many clients will not read early responses! Consider only issuing this response after the request data has been completely read!

有什么办法可以避免这个警告吗?

完整的源代码在这里:https://github.com/playframework/play-scala-fileupload-example/tree/2.6.x

这是文件上传处理请求的详细信息:

  type FilePartHandler[A] = FileInfo => Accumulator[ByteString, FilePart[A]]

  /**
   * Uses a custom FilePartHandler to return a type of "File" rather than
   * using Play's TemporaryFile class.  Deletion must happen explicitly on
   * completion, rather than TemporaryFile (which uses finalization to
   * delete temporary files).
   *
   * @return
   */
  private def handleFilePartAsFile: FilePartHandler[File] = {
    case FileInfo(partName, filename, contentType) =>
      val path: Path = Files.createTempFile("multipartBody", "tempFile")
      val fileSink: Sink[ByteString, Future[IOResult]] = FileIO.toPath(path)
      val accumulator: Accumulator[ByteString, IOResult] = Accumulator(fileSink)
      accumulator.map {
        case IOResult(count, status) =>
          logger.info(s"count = $count, status = $status")
          FilePart(partName, filename, contentType, path.toFile)
      }
  }

  /**
   * A generic operation on the temporary file that deletes the temp file after completion.
   */
  private def operateOnTempFile(file: File) = {
    val size = Files.size(file.toPath)
    logger.info(s"size = ${size}")
    Files.deleteIfExists(file.toPath)
    size
  }

  /**
   * Uploads a multipart file as a POST request.
   *
   * @return
   */
  def upload = Action(parse.multipartFormData(handleFilePartAsFile)) { implicit request =>
    val fileOption = request.body.file("name").map {
      case FilePart(key, filename, contentType, file) =>
        logger.info(s"key = ${key}, filename = ${filename}, contentType = ${contentType}, file = $file")
        val data = operateOnTempFile(file)
        data
    }

    Ok(s"file size = ${fileOption.getOrElse("no file")}")
  }

【问题讨论】:

    标签: scala playframework


    【解决方案1】:

    这是 Play 中的一个错误,我刚刚修复了 here。它仅在您发出分块请求时发生。它可以通过使用 Play 的 Netty 服务器后端而不是 Akka HTTP 来解决。

    【讨论】:

    • 我遇到了与@Eric 相同的问题,而且我还没有迁移到 v2.8 ...我想知道您所说的 Play 的 Netty 服务器后端是什么意思,而不是阿卡 HTTP ?可以举个例子吗?
    • 该错误已在一年多前发布的 2.6.22 和 2.7.1 中修复。您无需升级到 2.8。如果你在谷歌搜索“play netty server”,最上面的结果是playframework.com/documentation/2.8.x/NettyServer,它通过代码示例准确地解释了如何切换。
    • 我正在使用 play 2.6.3 :(
    • 所以升级到 2.6.25,这两个版本至少应该是二进制兼容的,但是 Play 团队非常小心,以确保它们不会在补丁修复中破坏东西。您不仅会修复此错误,还会获得大量其他错误修复,包括安全漏洞修复。说真的,Play 2.6.3 是在将近 3 年前发布的,在那段时间没有升级带有补丁修复的服务可以说是疏忽大意。
    • 好的,谢谢,我会尝试与负责升级游戏的团队交谈
    猜你喜欢
    • 1970-01-01
    • 2021-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-19
    • 2013-03-13
    相关资源
    最近更新 更多