java Spring MVC 多文件上传与 HTML5 多文件表单功能
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14924809/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring MVC multiple file upload with HTML5 multiple file form feature
提问by azpublic
I am trying to upload multiple files using spring 3.1.2 with @Controller and @RequestMapping.
我正在尝试使用带有 @Controller 和 @RequestMapping 的 spring 3.1.2 上传多个文件。
Here's what I did and my configuration.
这是我所做的和我的配置。
Html5 form :
Html5 形式:
<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.
提交表单确实会到达 addFileSystemImage 方法。locId 参数的数据在此处,但“文件”参数未绑定。无论我尝试过什么参数/字段名称/参数类型的组合,它都是系统性的。
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
HttpServletRequest 参数是一个 org.springframework.web.multipart.support.DefaultMultipartHttpServletRequest 并且它包含一个 multiPartFile 属性,它实际上包含文件数据。查看它在调试中的价值给了我
{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 ...
这意味着我的 files[] 确实在这里......但不知何故它没有正确通过数据绑定步骤......
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.
现在......我知道你会告诉我我可以从请求中检索数据......但我宁愿让它正常工作...... Sring方式...... :)并正确使用我的MultipartFile ArrayList人口稠密。
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?
我错过了什么吗?有没有人真正使这项工作正常?我该怎么做才能填充这个 ArrayList(甚至是一个常规的 Array )?
I came accross this solution Spring MVC with ajax file upload and MultipartFilewhich 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.
我通过 ajax 文件上传和 MultipartFile遇到了这个解决方案 Spring MVC,它和我做的事情几乎一样,但显然我一定做错了什么,因为这个解决方案对我不起作用。
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.
提前致谢。
回答by MetroidFan2002
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 namedfiles[] (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.
尽管感谢 Alex,您已经得到了答案,但我还是想详细说明一下。使用 Spring 绑定,表单字段绑定到它们在 HTML 中的“名称”属性。由于不可能有一个名为files[]的表单字段(如果用这种语法声明一个变量名,它的名字是 files,但它是一个声明类型的数组),Spring 无法匹配它 - 并且这种情况下的行为是忽略请求中的数据。
Using a type such as MultipartFile, you can use either a List named "files" or an array as the following examples:
使用 MultipartFile 等类型,您可以使用名为“files”的 List 或数组作为以下示例:
private List<MultipartFile> files;
private MultipartFile[] files;
With appropriate getters and setters, you can then access and mutate the file list accordingly.
使用适当的 getter 和 setter,您可以相应地访问和更改文件列表。
回答by gerrytan
Have included commons-fileupload dependency?
是否包含了 commons-fileupload 依赖项?
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3</version>
</dependency>
I tested the fileupload works fine even with ArrayList as the parameter type on the controller handler
我测试了文件上传工作正常,即使使用 ArrayList 作为控制器处理程序上的参数类型