Spring MVC多文件上传与HTML5多文件表单功能 [英] Spring MVC multiple file upload with HTML5 multiple file form feature

查看:192
本文介绍了Spring MVC多文件上传与HTML5多文件表单功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



下面是我做的和我的配置。



Html5格式:

 < form action =addFileSystemImage.foomethod = postenctype =multipart / form-data> 

< input class ='fileInput'type =filename =files []multiple =multiple/>

< input type =textvalue =13asdf12eadsfname =locId/>

< input type =submit/>

< / form>

控制器方法:

  @RequestMapping(value =/ publisher / addFileSystemImage.foo,method = RequestMethod.POST)
public @ResponseBody List< UploadedFile> addFileSystemImage(@RequestParam(files [])ArrayList< MultipartFile>文件,String locId,HttpServletRequest请求){

//在这里做很多voodoo火箭科学处理文件


$ / code>

我的conf:

 < mvc:annotation-driven /> 
< bean id =multipartResolverclass =org.springframework.web.multipart.commons.CommonsMultipartResolver>< / bean>

提交表单确实会到达addFileSystemImage方法。 locId参数的数据在这里,但文件参数没有绑定。这是系统性的null,无论是什么组合的参数/字段名称/参数类型我已经尝试。

HttpServletRequest参数是一个org.springframework.web.multipart.support。 DefaultMultipartHttpServletRequest,它包含一个multiPartFile属性,它实际上保存了文件数据。在debug中查看它的值给我

$ {$ p $ {code> {files [] = [org.springframework.web.multipart.commons.CommonsMultipartFile @ 16afd7f9,org.springframework.web.multipart.commons.CommonsMultipartFile@728c2811,org.springframework.web.multipart.commons.CommonsMultipartFile@4f9aaed7]}

这意味着我的文件[]确实在这里......但不知何故,它没有正确地传递数据绑定步骤...



现在...我知道你会告诉我,我可以检索请求中的数据...但我宁愿有这个工作正常... Sring的方式... :),并有我MultiListFile ArrayList人口稠密。



我错过了什么吗?有没有人真的使这个工作正常?我能做些什么来使这个ArrayList(或者甚至是一个常规的数组)填充?

我来到这个解决方案
这几乎与我一样的事情,但显然我必须做错了,因为这个解决方案不适合我。

注意:我确实设法使用单个文件上传。但我今天的挑战是一次获得多个文件。

任何帮助表示赞赏。



在此先感谢。

解决方案

尽管您已经获得你的答复感谢Alex,我只是想详细说明一下。使用Spring绑定,表单字段绑定到HTML中的名称属性。由于不可能有一个名为的文件[](如果用这种语法声明了一个变量名,它的名字是文件,但它是一个声明类型的数组) t匹配它 - 这种情况下的行为是忽略请求中的数据。

使用类型如MultipartFile,您可以使用名为文件或数组,如以下示例所示:

  private List< MultipartFile>文件; 
私人MultipartFile []文件;

使用合适的getter和setter,您可以访问并相应地修改文件列表。


I am trying to upload multiple files using spring 3.1.2 with @Controller and @RequestMapping.

Here's what I did and my configuration.

Html5 form :

<form action="addFileSystemImage.foo" method="post" enctype="multipart/form-data">

    <input class='fileInput' type="file" name="files[]" multiple="multiple" />

    <input type="text" value="13asdf12eadsf" name="locId"/>

    <input type="submit" />

</form>

Controller method :

@RequestMapping(value="/publisher/addFileSystemImage.foo", method=RequestMethod.POST)
public @ResponseBody List<UploadedFile> addFileSystemImage(@RequestParam("files[]") ArrayList<MultipartFile> files, String locId, HttpServletRequest request) {

 //do lotsa voodoo rocket science here to process the files  

}

my conf :

 <mvc:annotation-driven />
 <context:component-scan base-package="foo.package"></context:component-scan>
 <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"></bean>

Submitting the form does get to the addFileSystemImage method. The data for locId argument is here, but the "files" argument is not bound. It is systematically null no matter what combination of argument / field names / argument types I have tried.

The HttpServletRequest argument is a org.springframework.web.multipart.support.DefaultMultipartHttpServletRequest and it holds a multiPartFile attribute which actually holds the file data. Looking at its value in debug gives me

{files[]=[org.springframework.web.multipart.commons.CommonsMultipartFile@16afd7f9, org.springframework.web.multipart.commons.CommonsMultipartFile@728c2811, org.springframework.web.multipart.commons.CommonsMultipartFile@4f9aaed7]}

which means my files[] is indeed here ... but somehow it did not pass the data binding step properly ...

Now ... I know you're gonna tell me I can retrieve the data from the request ... but I'd rather have this working properly ... the Sring way... :) and have my ArrayList of MultipartFile properly populated.

Am I missing something ? Has anyone actually made this work properly ? What can I do to have this ArrayList (or even an regular Array ) populated?

I came accross this solution Spring MVC with ajax file upload and MultipartFile which does pretty much the same thing as I am but obviously I must be doing something wrong since this solution is not working for me.

Note : I did manage to get it working with single file uploads. But my challenge today is to get multiple files at once.

Any help appreciated.

Thanks in advance.

解决方案

Although you've already gotten your answer thanks to Alex, I'd just like to elaborate a bit. With Spring binding, form fields are bound to their "name" attributes in the HTML. Since it is impossible to have a form field named files[] (if one declares a variable name with that syntax, its name is files, but it is an array of the declaring type), Spring couldn't match it up - and the behavior in that case is to disregard the data in the request.

Using a type such as MultipartFile, you can use either a List named "files" or an array as the following examples:

private List<MultipartFile> files;
private MultipartFile[] files;

With appropriate getters and setters, you can then access and mutate the file list accordingly.

这篇关于Spring MVC多文件上传与HTML5多文件表单功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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