簡體   English   中英

在完全讀取請求之前,Akka-Http文件上傳發送響應

[英]Akka-Http File Upload sending response before request is completely read

我的akka​​-http服務器中有以下路線:

def tempDestination(fileInfo: FileInfo): File = {
    log.info(s"File info for upload: [$fileInfo]")
    File.createTempFile(fileInfo.fileName, ".tmp")
}
...
post {
    log.info("Inside the file upload path")
    fileUpload("zip") {
      case (fileInfo, bytes) =>
        val dest = tempDestination(fileInfo)
        val uploadedF: Future[(FileInfo, File)] =
          bytes
            .runWith(FileIO.toPath(dest.toPath))
            .map(_ => (fileInfo, dest))

        onSuccess(uploadedF) { (info, file) =>
            val fileCreated: Future[MyFile] = (fileRegistryActor ? NewFile(file)).mapTo[MyFile]
            complete((StatusCodes.Created, fileCreated))
        }
    }
}

通過表單將文件上傳到此文件后,我在日志中看到了該文件:

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!

我認為uploadedF直到水槽的IOResult創建並完成未來將無法完成。 我想念什么? 確定我的整個請求是否已被讀取以及文件是否已完全寫入磁盤的適當機制是什么?

因此,為我解決此問題的方法是將路線從just post更改為post & extractRequest

def tempDestination(fileInfo: FileInfo): File = {
    log.info(s"File info for upload: [$fileInfo]")
    File.createTempFile(fileInfo.fileName, ".tmp")
}
...
(post & extractRequest) { _ =>
    log.info("Inside the file upload path")
    fileUpload("zip") {
      case (fileInfo, bytes) =>
        val dest = tempDestination(fileInfo)
        val uploadedF: Future[(FileInfo, File)] =
          bytes
            .runWith(FileIO.toPath(dest.toPath))
            .map(_ => (fileInfo, dest))

        onSuccess(uploadedF) { (info, file) =>
            val fileCreated: Future[MyFile] = (fileRegistryActor ? NewFile(file)).mapTo[MyFile]
            complete((StatusCodes.Created, fileCreated))
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM