如何处理通过Spray / Scala / Java中的HTML表单上传的文件? [英] How can I process a file uploaded through an HTML form in Spray/Scala/Java?

查看:859
本文介绍了如何处理通过Spray / Scala / Java中的HTML表单上传的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 Spray 编写的应用程序,而且我有一个包含< input type =filename =foo> 表单元素被发送到 / fileUpload p>

我使用下面的代码设置了侦听路径 / fileUpload 的路径:

  path(fileUpload){
get {ctx => {
val请求:HttpRequest = ctx.request
//以某种方式处理文件,请求?
ctx.complete(File Uploaded)
}}
}

我无法弄清楚如何获得POST正文并获得文件的句柄,而且我也找不到任何在线的例子。



它必须有可能收到一个文件,并用Spray或甚至通过简单的Scala或Java处理,但我不知道如何去做。



任何人都可以帮忙吗? b
谢谢!

解决方案



  post {
content(as [MultipartFormData]){
def extractOriginalText(formData:MultipartFormData):String = {
formData.parts.mapValues {(bodyPart)=>
bodyPart.content.map {
(content)=> new String(content.buffer)
}
} .values.flatten.foldLeft()(_ + _)
}
formData =>
_.complete(
extractOriginalText(formData)
);



$ b $ p
$ b

如果您将纯文本文件上载到包含此代码的服务,它会把原来的文字重新写回来。我已经与ajax上传一起运行;它应该也使用一个老式的文件上传形式。

在我看来,必须有一个更简单的方法来做到这一点,特别是内容深嵌套相当笨重。如果您发现简化,请告诉我。
$ b

更新(thx akauppi):

  entity(as [MultipartFormData]){formData => 
complete(formData.fields.map {_.entity.asString} .flatten.foldLeft()(_ + _))
}
pre>

I have an application written using Spray, and I have a page which has an <input type="file" name="foo"> form element that gets POSTed to /fileUpload.

I have a Spray route set up to listen to the path /fileUpload using this code:

path("fileUpload") {
  get { ctx => {
    val request: HttpRequest = ctx.request
    //Process the file, somehow with request?
    ctx.complete("File Uploaded")
  }}
}

I can't figure out how to get the POST body and get a handle on the file, and I can't find any examples online.

It must be possible to receive a file and process it with Spray or even through simple Scala or Java, but I don't know how to do it.

Can anybody help?
Thanks!

解决方案

It's possible with Spray, although I haven't checked if streaming works properly. I fiddled a bit and got this working:

  post {
    content(as[MultipartFormData]) {
      def extractOriginalText(formData: MultipartFormData): String = {
        formData.parts.mapValues { (bodyPart) =>
          bodyPart.content.map{
            (content) => new String(content.buffer)
          }
        }.values.flatten.foldLeft("")(_ + _)
      }
      formData =>
        _.complete(
          extractOriginalText(formData)
        );
    }

If you upload a plain text file to a service that has this code in it, it coughs the original text back up. I've got it running together with an ajax upload; it should also work with an old fashioned file upload form.

It seems to me that there must be an easier way to do this, particularly the deep nesting of the content is rather clunky. Let me know if you find a simplification.

UPDATE (thx akauppi):

entity(as[MultipartFormData]) { formData => 
  complete( formData.fields.map { _.entity.asString }.flatten.foldLeft("")(_ + _) ) 
}

这篇关于如何处理通过Spray / Scala / Java中的HTML表单上传的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆