我正在尝试使用带有@Controller 和@RequestMapping 的spring 3.1.2 上传多个文件。
这是我所做的和我的配置。
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>
控制器方法:
@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
}
我的会议:
<mvc:annotation-driven />
<context:component-scan base-package="foo.package"></context:component-scan>
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"></bean>
提交表单确实会到达 addFileSystemImage 方法。locId 参数的数据在这里,但“文件”参数未绑定。无论我尝试过哪种参数/字段名称/参数类型的组合,它都系统地为空。
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]}
这意味着我的 files[] 确实在这里......但不知何故它没有正确通过数据绑定步骤......
现在......我知道你会告诉我我可以从请求中检索数据......但我宁愿让它正常工作...... Sring方式...... :) 并让我的 MultipartFile ArrayList 正确人口稠密。
我错过了什么吗?有没有人真正使这项工作正常进行?我能做些什么来填充这个 ArrayList (甚至是一个常规的 Array )?
我遇到了这个 带有 ajax 文件上传和 MultipartFile 的 Spring MVC解决方案,它和我做的事情几乎一样,但显然我一定做错了,因为这个解决方案不适合我。
注意:我确实设法让它与单个文件上传一起工作。但我今天的挑战是一次获取多个文件。
任何帮助表示赞赏。
提前致谢。