In working on the multi-file upload support in Spring (see Spring Multipart Issue), I had to code up a simple example of how it works now. It took me a little searching to figure out how to use multipart support with the new controller annotation configurations, though it turns out to be really easy. I figured I’d post a quick summary of how it’s done.
With your standard file upload form:
1 2 3 4 |
posting to your uploading controller, which is defined as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | @Controller @RequestMapping("/upload.do") public class UploadController { @SuppressWarnings("unchecked") @RequestMapping(method=RequestMethod.POST) public ModelAndView upload(@RequestParam("file") final MultipartFile multipartFile){ final ModelAndView mav = new ModelAndView("done"); // do stuff with the file return mav; } } |
which just seems way too simple. Don’t forget to configure the MultipartResolver along with the rest of your spring-mvc config:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <context:annotation-config /> <context:component-scan base-package="com.stehno.springmulti" /> <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" /> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="100000"/><!-- Max size in bytes. --> </bean> |
and that’s it.
Like I said, nothing real exciting here, really just a quick setup starting point.
Note: my fix of the multiple-file upload support is on hold until Spring 3.0 is officially released. I know what needs to be done, it’s just a waiting game now.
Popularity: 88% [?]
