Thursday, 24 February 2011

FileUpload with multipart/form-data in Spring 2.5.x and Annotations

Firstly a qualifier, This is for Spring 2.5.x with annotations.
I have not had the pleasure of upgrading to Spring 3.X yet.

There is some excellent documentation on File Upload with Spring.

However for an annotation driven Controller it misses out one important step, namely the @InitBinder annotation.

public void initBinder(ServletRequestDataBinder aBinder) throws ServletException {
// to actually be able to convert Multipart instance to a String
// we have to register a custom editor
aBinder.registerCustomEditor(String.class, new StringMultipartFileEditor());
// now Spring knows how to handle multipart object and convert them

No comments: