File Upload (multiplart) Example Using Spring

In this article, we will demonstrate a step by step example of how to upload a file using Spring and multiplart request. Spring has built-in multipart support to handle fileuploads in web applications. The design for the multipart support is done with pluggable MultipartResolver objects, defined in the org.springframework.web.multipart package. Out of the box, Spring provides a MultipartResolver for use with Commons FileUpload How uploading files is supported will be described here.

Using the MultipartResolver

The following example shows how to use the CommonsMultipartResolver. Of course you also need to put the commons-fileupload.jar in your classpath for the multipart resolver to work.

<bean id="multipartResolver"
<!-- one of the properties available; the maximum file size in bytes -->
   <property name="maxUploadSize" value="100000"/>

Handling a file upload in a form

you create a form with an upload field (see immediately below), then let Spring bind the file onto your form(backing object). To actually let the user upload a file, we have to create a (HTML) form:

    <title>Upload a file using Spring</title>
    <h1>Please upload a file</h1>
    <form method="post" action="upload.form" enctype="multipart/form-data">
      <input type="file" name="file" />
      <input type="submit" />

As you can see, we've created a field named after the property of the bean that holds the byte[]. Furthermore we've added the encoding attribute (enctype="multipart/form-data") which is necessary to let the browser know how to encode the multipart fields (do not forget this!).

Spring Configuration File

So, to be able to upload files using a (HTML) form, declare the resolver, a url mapping to a controller that will process the bean, and the controller itself.

<!-- lets use the Commons-based implementation of the MultipartResolver interface -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
  <property name="mappings">
<bean id="fileUploadController" class="examples.FileUploadController">
  <property name="commandClass" value="examples.FileUploadBean" />
  <property name="formView" value="fileuploadform" />
  <property name="successView" value="confirmation" />


After that, create the controller and the actual class to hold the file property.

public class FileUploadController extends SimpleFormController {
  protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response,
Object command, BindException errors) throws ServletException, IOException {
// cast the bean
FileUploadBean bean = (FileUploadBean) command;
let's see if there's content there
byte[] file = bean.getFile();
if (file == null) {
// hmm, that's strange, the user did not upload anything
// well, let's do nothing with the bean for now and return
return super.onSubmit(request, response, command, errors);

  protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws ServletException {
    // to actually be able to convert Multipart instance to byte[]
    // we have to register a custom editor
    binder.registerCustomEditor(byte[].class, new ByteArrayMultipartFileEditor());
    // now Spring knows how to handle multipart object and convert them

public class FileUploadBean {
  private byte[] file;

  public void setFile(byte[] file) {
    this.file = file;

  public byte[] getFile() {
    return file;

As you can see, the FileUploadBean has a property typed byte[] that holds the file. The controller registers a custom editor to let Spring know how to actually convert the multipart objects the resolver has found to properties specified by the bean. In this example, nothing is done with the byte[] property of the bean itself, but in practice you can do whatever you want (save it in a database, mail it to somebody, etc).

No comments:

Post a Comment