上载播放文件:警告:在收到请求结束之前发送2xx“早期"响应 [英] Play file upload : warn: Sending an 2xx 'early' response before end of request was received

查看:120
本文介绍了上载播放文件:警告:在收到请求结束之前发送2xx“早期"响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过播放Scala文件上传示例"进行实验,我收到以下警告:

Experimenting with the "Play Scala File Upload Example", I've got the following warning:

[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!

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

Is there any way to avoid this warning ?

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

The full source code is available here: https://github.com/playframework/play-scala-fileupload-example/tree/2.6.x

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

This is the detail of the file upload handling request:

  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")}")
  }

推荐答案

这是Play中的一个错误,我刚刚修复了

This is a bug in Play that I just fixed here. It only happens when you make chunked requests. It can be worked around by using Play's Netty server backend instead of Akka HTTP.

这篇关于上载播放文件:警告:在收到请求结束之前发送2xx“早期"响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆