Thursday, August 5, 2010

Spring: Enabling Multipart Form Upload Http Request Processing

In Spring you can write http controller using @Controller annotation at class level to process http requests. The methods defined in controller can take HttpServletRequest as one of the arguments.


@Controller
@RequestMapping("/customers")
class CustomerController {

    @RequestMapping(method = RequestMethod.POST)
    public void postCustomer(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //processing code goes here
    }
}


Handling Form Multipart Request:

If you like to handle form multipart requests, spring provides an easy way to handle this by using MultipartHttpServletRequest. This allows you to access multipart files available in the request.

To use MultipartHttpServletRequest, you can specify CommonsMultipartResolver bean in your spring xml configuration file.


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


Once you specify you can change the signature of your controller methods to use
MultipartHttpServletRequest in place of HttpservletRequest:


@Controller
@RequestMapping("/customers")
class CustomerController {

    @RequestMapping(method = RequestMethod.POST)
    public void postCustomer(MultipartHttpServletRequest request, HttpServletResponse response) throws Exception {
        //processing code goes here
    }
}


Promote your blog

1 comment:

  1. Heya¡­my very first comment on your site. ,I have been reading your blog for a while and thought I would completely pop in and drop a friendly note. . It is great

    stuff indeed. I also wanted to ask..is there a way to subscribe to your site via email?







    Form Processing

    ReplyDelete