【问题标题】:Akka-HTTP: File UploadAkka-HTTP:文件上传
【发布时间】:2015-12-31 16:55:54
【问题描述】:

我正在尝试使用 akka http 实现简单的文件上传。 我的尝试如下:

    import akka.actor.ActorSystem
    import akka.event.{LoggingAdapter, Logging}
    import akka.http.scaladsl.Http
    import akka.http.scaladsl.model.{HttpResponse, HttpRequest}
    import akka.http.scaladsl.model.StatusCodes._
    import akka.http.scaladsl.server.Directives._
    import akka.stream.{ActorMaterializer, Materializer}
    import com.typesafe.config.Config
    import com.typesafe.config.ConfigFactory
    import scala.concurrent.{ExecutionContextExecutor, Future}
    import akka.http.scaladsl.model.StatusCodes
    import akka.http.scaladsl.model.HttpEntity
    import java.io._
    import akka.stream.io._

    object UploadTest extends App {
      implicit val system = ActorSystem()
      implicit val executor = system.dispatcher
      implicit val materializer = ActorMaterializer()

      val config = ConfigFactory.load()
      val logger = Logging(system, getClass)

      val routes = {
        pathSingleSlash {
          (post & extractRequest) { 
            request => {
              val source = request.entity.dataBytes
              val outFile = new File("/tmp/outfile.dat")
              val sink = SynchronousFileSink.create(outFile)
              source.to(sink).run()
              complete(HttpResponse(status = StatusCodes.OK))
            }
          }
        }
      }

      Http().bindAndHandle(routes, config.getString("http.interface"), config.getInt("http.port"))

    }

这段代码有几个问题:

  1. 无法上传大于配置的实体大小的文件: Request Content-Length 24090745 exceeds the configured limit of 8388608
  2. 连续执行两次上传会导致dead letters encountered. 异常。

克服大小限制的最佳方法是什么?如何正确关闭文件,以便后续上传会覆盖现有文件(暂时忽略并发上传)?

【问题讨论】:

    标签: scala upload akka akka-stream


    【解决方案1】:

    对于第 2 点,我认为 source.to(sink).run() 异步执行操作。它实现了Future。因此,您的 HTTP 请求可能会在文件写入完成之前返回,因此如果您在第一个请求返回后立即在客户端开始第二次上传,则第一个请求可能尚未完成对文件的写入。

    您可以使用 onCompleteonSuccess 指令仅在未来完成时完成 http 请求:

    http://doc.akka.io/docs/akka-stream-and-http-experimental/1.0-M2/scala/http/directives/alphabetically.html

    http://doc.akka.io/docs/akka-stream-and-http-experimental/1.0/scala/http/routing-dsl/directives/future-directives/onSuccess.html

    编辑:

    对于内容长度问题,您可以做的一件事是增加application.conf 中该属性的大小。默认为:

    akka.server.parsing.max-content-length = 8m
    

    http://doc.akka.io/docs/akka-stream-and-http-experimental/1.0/java/http/configuration.html

    【讨论】:

    • 谢谢,使用 onSuccess 解决了“遇到死信”的问题。
    • 添加了一个针对其他问题的想法的编辑
    • 嗨,谢谢,我了解到将 max-content-length 设置为更大的值并不意味着整个内容都缓存在内存中。所以这是解决这个问题的安全方法。我会赞成你的回答,但我没有足够的学分:-(
    • 我认为你可以接受它作为答案... :)
    【解决方案2】:

    总结 mattinbits cmets,以下解决方案有效:

    1. 增加akka.server.parsing.max-content-length
    2. 使用onSuccess

    这是代码的sn-p:

    val routes = {
      pathSingleSlash {
        (post & extractRequest) { 
          request => {
            val source = request.entity.dataBytes
            val outFile = new File("/tmp/outfile.dat")
            val sink = SynchronousFileSink.create(outFile)
            val repl = source.runWith(sink).map(x => s"Finished uploading ${x} bytes!")
            onSuccess(repl) { repl =>
              complete(HttpResponse(status = StatusCodes.OK, entity = repl))
            }
          }
        }
      }
    

    【讨论】:

      猜你喜欢
      • 2018-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-19
      相关资源
      最近更新 更多