如何使用Spray上传图像?

时间:2014-11-19 20:16:10

标签: scala akka spray

如何使用Spray处理图像上传?一个简单的例子就足够了。目前,我尝试在Spray + Akka上关注与Typesafe Activator捆绑的tutorial,但是当我升级到最新的喷雾版本时出现错误。

特别是第42行:

data.fields.get("files[]") match {

抱怨它"无法解析符号get"。

随后是RegistrationService.scala的完整来源,但您也可以在此处找到github repo的完整来源:https://github.com/eigengo/activator-akka-spray/blob/master/src/main/scala/api/RegistrationService.scala

package api

import spray.routing.Directives
import scala.concurrent.ExecutionContext
import akka.actor.ActorRef
import core.{User, RegistrationActor}
import akka.util.Timeout
import RegistrationActor._
import spray.http._
import core.User
import core.RegistrationActor.Register
import scala.Some

class RegistrationService(registration: ActorRef)(implicit executionContext: ExecutionContext)
  extends Directives with DefaultJsonFormats {

  case class ImageUploaded(size: Int)

  import akka.pattern.ask
  import scala.concurrent.duration._
  implicit val timeout = Timeout(2.seconds)

  implicit val userFormat = jsonFormat4(User)
  implicit val registerFormat = jsonFormat1(Register)
  implicit val registeredFormat = jsonObjectFormat[Registered.type]
  implicit val notRegisteredFormat = jsonObjectFormat[NotRegistered.type]
  implicit val imageUploadedFormat = jsonFormat1(ImageUploaded)

  implicit object EitherErrorSelector extends ErrorSelector[NotRegistered.type] {
    def apply(v: NotRegistered.type): StatusCode = StatusCodes.BadRequest
  }

  val route =
    path("register") {
      post {
        handleWith { ru: Register => (registration ? ru).mapTo[Either[NotRegistered.type, Registered.type]] }
      }
    } ~
    path("register" / "image") {
      post {
        handleWith { data: MultipartFormData =>
          data.fields.get("files[]") match {
            case Some(imageEntity) =>
              val size = imageEntity.entity.buffer.length
              println(s"Uploaded $size")
              ImageUploaded(size)
            case None =>
              println("No files")
              ImageUploaded(0)
          }
        }
      }
    }

}

为了完整性,这是我的构建文件:

name := """activator-akka-spray"""

version := "1.0"

scalaVersion := "2.11.2"

resolvers += "spray repo" at "http://repo.spray.io"

resolvers += "spray nightlies" at "http://nightlies.spray.io"

libraryDependencies ++= {
    val sprayV = "1.3.2"
    val akkaV = "2.3.6"
    val sprayJsonV = "1.3.1"
    val logbackV = "1.1.2"
    val specsV = "2.4.11"
    Seq(
      "com.typesafe.akka"  %% "akka-actor"       % akkaV,
      "com.typesafe.akka"  %% "akka-slf4j"       % akkaV,
      "io.spray"           %% "spray-can"        % sprayV,
      "io.spray"           %% "spray-routing"    % sprayV,
      "io.spray"           %% "spray-json"       % sprayJsonV,
      "org.specs2"         %% "specs2-core"      % specsV      % "test",
      "io.spray"           %% "spray-testkit"    % sprayV      % "test",
      "com.typesafe.akka"  %% "akka-testkit"     % akkaV       % "test",
      // Non-Scala dependencies
      "com.novocode" % "junit-interface" % "0.11" % "test",
      "ch.qos.logback"     % "logback-classic"  % logbackV
    )
}

scalacOptions ++= Seq(
  "-unchecked",
  "-deprecation",
  "-Xlint",
  "-Ywarn-dead-code",
  "-language:_",
  "-target:jvm-1.7",
  "-encoding", "UTF-8"
)

testOptions += Tests.Argument(TestFrameworks.JUnit, "-v")

任何帮助表示感谢。

1 个答案:

答案 0 :(得分:3)

使用data.get("files[]") match ...

api doc:http://spray.io/documentation/1.1-SNAPSHOT/api/#spray.http.MultipartFormData