如何在akka-http中上传文件和获取表单字段

rks48beu  于 2022-11-06  发布在  其他
关注(0)|答案(1)|浏览(209)

我正在尝试通过akka-http上传一个文件,并且已经使用以下代码段使其工作

def tempDestination(fileInfo: FileInfo): File =
  File.createTempFile(fileInfo.fileName, ".tmp")

val route =
  storeUploadedFile("csv", tempDestination) {
    case (metadata, file) =>      
      //Do my operation on the file.
      complete("File Uploaded. Status OK")
  }

但我还想以发布的形式发送param 1/param 2。
我尝试了下面的方法,它可以工作,但是我必须通过URL(http://host:port/csv-upload?userid = arvind)发送参数。

(post & path("csv-upload")) {
   storeUploadedFile("csv", tempDestination) {
     case (metadata, file) =>
       parameters('userid) { userid =>
          //logic for processing the file
          complete(OK)
       }
   }
}

对文件大小的限制是大约200-300 MB。

akka{
     http{
         parsing{
             max-content-length=200m
         }
     }
 }

有没有办法,我可以通过formFields指令获得参数?
我尝试了以下方法

fileUpload("csv") {
        case (metadata, byteSource) =>
          formFields('userid) { userid =>
            onComplete(byteSource.runWith(FileIO.toPath(Paths.get(metadata.fileName)))) {
              case Success(value) =>
                logger.info(s"${metadata}")
complete(StatusCodes.OK)
              case Failure(exception) =>
                complete("failure")

但是,对于上面的代码,我遇到了以下异常

java.lang.IllegalStateException: Substream Source cannot be materialized more than once
    at akka.stream.impl.fusing.SubSource$$anon$13.setCB(StreamOfStreams.scala:792)
    at akka.stream.impl.fusing.SubSource$$anon$13.preStart(StreamOfStreams.scala:802)
    at akka.stream.impl.fusing.GraphInterpreter.init(GraphInterpreter.scala:306)
    at akka.stream.impl.fusing.GraphInterpreterShell.init(ActorGraphInterpreter.scala:593)

谢谢你,阿文

qyyhg6bp

qyyhg6bp1#

我得到了这个工作的东西一样:

path("upload") {

    formFields(Symbol("payload")) { payload =>
      println(s"Server received request with additional payload: $payload")

      def tempDestination(fileInfo: FileInfo): File = File.createTempFile(fileInfo.fileName, ".tmp.server")

      storeUploadedFile("binary", tempDestination) {
        case (metadataFromClient: FileInfo, uploadedFile: File) =>
          println(s"Server stored uploaded tmp file with name: ${uploadedFile.getName} (Metadata from client: $metadataFromClient)")
          complete(Future(FileHandle(uploadedFile.getName, uploadedFile.getAbsolutePath, uploadedFile.length())))
      }
    }
  }

完整示例:https://github.com/pbernet/akka_streams_tutorial/blob/master/src/main/scala/akkahttp/HttpFileEcho.scala

相关问题