Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
2  
Did you try to declare the "files" argument as a List or Multipart[] files? I don't think that the Spring backing list is an ArrayList and maybe the cast fails when Spring tries to bind the method arguments. – Alex Calugarescu Feb 17 at 19:13
I managed to get something like this working in the past. I remember you have to also include commons-fileupload dependency in your pom. Maybe try that? – gerrytan Feb 18 at 4:10
Stupid me ... It did work with List and Array ... I must've lost track of what I did during all the testing... Thanks Alex. You can answer the question and I'll give you point if you want. Cheers ! – azpublic Feb 18 at 8:19

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.