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

1
  • have a look here Commented Feb 23, 2015 at 9:13

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.