1

I want to upload file and do process by using Spring MVC3. i tried with

@RequestMapping(value = "uploadAction.do")
public ModelAndView upload(
@RequestParam("file") CommonsMultiPartFile file
)
{

System.out.println(file);
ModelAndView view = new ModelAndView();
return view;
}

but it is not working and i have confused with @RequestParam and @ModelAttribute so please help me

Sasikumar Murugesan
  • 4,412
  • 10
  • 51
  • 74
  • have a look [here](http://stackoverflow.com/questions/20162474/how-do-i-receive-a-file-upload-in-spring-mvc-using-both-multipart-form-and-chunk) – Bond - Java Bond Feb 23 '15 at 09:13

2 Answers2

2

The upload functionality depends on several factors factors. As by the docs, following are the things you must ensure

Make a POST request. File upload should be a POST request

@RequestMapping(value = "uploadAction.do", method=RequestMethod.POST)

Enable Spring multipart handling by adding a multipart resolver to the web application’s context

     <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
         <!-- one of the properties available; the maximum file size in bytes -->
         <property name="maxUploadSize" value="100000"/>
    </bean>

Ensure commons-fileupload.jar is on your classpath, if you're using maven the following should cover you

    <!-- File Upload -->
    <dependency>
        <groupId>commons-fileupload</groupId>
        <artifactId>commons-fileupload</artifactId>
        <version>1.2.2</version>
    </dependency>
    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.0.1</version>
    </dependency>

with all this in place, your mapping should work well, providing that your form is OK, here is an example

    <form id="fileuploadForm" action="/uploadAction.do" method="POST" enctype="multipart/form-data" class="cleanform">
        <input id="file" type="file" name="file" />
        <p><button type="submit">Upload</button></p>        
    </form>

note also that its always better to program against an interface, by changing the argument type to

public ModelAndView upload(@RequestParam("file") MultipartFile file) {

you'll delegate injecting the implementation to the framework. The benefit is that you can write file upload test using spring mvc test framework, in which case the framework will insert the mocked implementation for the MultipartFile interface

Master Slave
  • 27,771
  • 4
  • 57
  • 55
0

Something like this:

@RequestMapping(value="/upload", method=RequestMethod.POST)
    public String upload( @RequestParam("upload") 
                          MultipartFile multipartFile
                          ...                         
){          
    ....
}

and in the form enctype is required:

<form id="command" name="command" method="POST" action="/upload" enctype="multipart/form-data">
     ....
      <input id="upload" type="file" name="photo">
     ....
</form>
ema
  • 891
  • 11
  • 21