gpt4 book ai didi

scala - 喷雾文件上传: Unmarshalling MultiPartForm

转载 作者:行者123 更新时间:2023-12-02 15:31:46 28 4
gpt4 key购买 nike

我正在学习Spray,使用spray-can和spray-httpx(没有spray-routing)来接受上传的文件。我提出了以下建议:

  def receive = {
...

case HttpRequest(POST, Uri.Path("/upload"), _, entity, _) =>
object mp extends MultipartUnmarshallers
mp.MultipartFormDataUnmarshaller(entity).foreach{ part =>
for{
fname <- part.fields.get("Filename").map(_.entity.asString)
fbody <- part.fields.get("Filedata").map(_.entity.buffer)
}{
println(fname+ ": " +fbody.length)
}
}
sender ! HttpResponse(status = 200)

...
}

这可行,但我认为多部分解码器不应该以这种方式使用。有更优雅的方法吗?

最佳答案

这可能不是最优雅的,但是,它有效

val e = spray.httpx.unmarshalling.FormDataUnmarshallers.MultipartFormDataUnmarshaller.apply(request.entity)

e.fold(error => {
throw new IllegalArgumentException("could not unmarshall multipart form data")
},
formdata => {
for (part <- formdata.fields) {
//do what you want with the parts
}
})

关于scala - 喷雾文件上传: Unmarshalling MultiPartForm,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16651078/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com