我想用@Controller和@RequestMapping使用彈簧3.1.2上傳多個文件。春季MVC多文件上傳與HTML5多文件格式功能
這是我做的和我的配置。
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
}
我的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>
提交表單並得到了addFileSystemImage方法。 locId參數的數據在這裏,但「文件」參數沒有綁定。無論我嘗試過的參數/字段名稱/參數類型的組合如何,它都是系統化的。
HttpServletRequest參數是一個org.springframework.web.multipart.support.DefaultMultipartHttpServletRequest,它包含一個multiPartFile屬性,它實際上包含文件數據。看着它在調試值給我
{files[]=[[email protected]6afd7f9, [email protected]28c2811, [email protected]f9aaed7]}
這意味着我的文件[]的確是在這裏...但不知何故,沒有通過正確的數據綁定步驟...
現在...我知道你會告訴我,我可以從請求中檢索數據...但我寧願讓這個工作正常...... Sring的方式...... :)並讓我的ArrayList的MultipartFile正確填充。
我缺少的東西?有沒有人真正做到這一點?我能做些什麼來讓這個ArrayList(或者甚至是一個常規的數組)被填充?
我來到這個解決方案 Spring MVC with ajax file upload and MultipartFile它與我做的事情幾乎一樣,但顯然我必須做錯事,因爲這個解決方案不適合我。
注意:我確實設法使用單個文件上傳。但我今天的挑戰是一次獲得多個文件。
任何幫助表示讚賞。
在此先感謝。
您是否嘗試將「files」參數聲明爲List或Multipart []文件?我不認爲Spring支持列表是一個ArrayList,並且當Spring嘗試綁定方法參數時可能會失敗。 – 2013-02-17 19:13:32
我設法得到了這樣的工作在過去。我記得你還必須在你的pom中包含commons-fileupload依賴。也許試試? – gerrytan 2013-02-18 04:10:50
愚蠢的我......它和List和Array一起工作......我一定失去了在所有測試過程中所做的事......感謝Alex。你可以回答這個問題,如果你願意,我會給你點。乾杯! – azpublic 2013-02-18 08:19:53