Archive

Posts Tagged ‘Multipart’

File upload using REST Spring MVC

April 21, 2010 7 comments

A quick note on developing an HTTP POST based web service using Spring 3 MVC and its testing using curl command line program.

To me the most important resides in the configuration file:

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

<property name="maxUploadSize" value="300000"/><!-- Max size in bytes. -->

</bean>

The web service itself is configured like this:

@RequestMapping(value = "/fileUpload", method = RequestMethod.POST)
@ResponseBody
public myObject upload (
@RequestParam("file") MultipartFile multipartFile,
HttpServletRequest request) {...

and the curl command is:

curl -F "file=@file.jpg" http://localhost:8080/fileUpload

Edit 4/12/2011: just noticed there exists a nice working example at springsource dealing with this (and much more), here