0
は、Excelシートをアップロードし、ローカルに保存するため、サーバ側でアッカのhttpでExcelファイルをアップロードする方法をマイクロサービス
def uploadFile(fileData: Multipart.FormData) = {
println(" uploadFile ")
// path("user"/"upload"/"file") {
/* (post & entity(as[Multipart.FormData])) { fileData =>*/
complete {
val fileName = UUID.randomUUID().toString
val temp = System.getProperty("java.io.tmpdir")
val filePath = temp + "/" + fileName+".xls"
// var filePath = current.configuration.getString("upload.file.path").get + "/" + fileName
println(fileData.getParts() + " - " + fileData.getMediaType() + " filePath " + filePath + " fileName " + fileName)
val processingFileUpload = processFile(filePath, fileData)
/*val poResult = Await.result(processingFileUpload, 50 seconds)
println(" processFile " + poResult)*/
processingFileUpload.map { fileSize =>
HttpResponse(StatusCodes.OK, entity = s"File successfully uploaded. Fil size is $fileSize")
}.recover {
case ex: Exception => HttpResponse(StatusCodes.InternalServerError, entity = "Error in file uploading")
}
// }
// }
}
}
をアッカのHTTPを使用していますし、私のprocessFileは
private def processFile(filePath: String, fileData: Multipart.FormData) = {
val fileOutput = new FileOutputStream(filePath)
println(" fileOutput " + fileOutput+" fileDatas "+fileData.parts.module)
// fileData.parts.mapAsync(1) { bodyPart =>
fileData.parts.mapAsyncUnordered(1) { bodyPart =>
println(" bodyPartLog " + bodyPart)
def writeFileOnLocal(array: Array[Byte], byteString: ByteString): Array[Byte] = {
println(" arraysdss " + array)
val byteArray: Array[Byte] = byteString.toArray
fileOutput.write(byteArray)
println(" sdssasx " + byteArray)
array ++ byteArray
}
bodyPart.entity.dataBytes.runFold(Array[Byte]())(writeFileOnLocal)
}.runFold(0)(_ + _.length)
}
です私はmapAsyncとmapAsyncUnorderedの両方を尋ねました。ファイルをアップロードする際にエラーが発生しました。例外に直接ジャンプしました。サービスをアップロードして自分のサーバーにデータを書き込む方法を教えてください。
例外は何ですか?あなたは、おそらく、あなたのprocessFile実装からこれを使うように切り替える必要があります:https://doc.akka.io/api/akka/current/akka/stream/scaladsl/FileIO$.htmlあなたの実装は現在、出力を閉じない、メモリに全体を読み込む、データの複数のコピーを作成するなどの問題 –