title: Spring 3.0 MultipartFile upload
body: <p>I am converting Java web application to Spring framework and appreciate some advice on the issues I am facing with the file upload. Original code was written using org.apache.commons.fileupload.</p>
  
  <ol>
  <li><p>Does Spring MultipartFile wraps org.apache.commons.fileupload or I can exclude this dependency from my POM file?</p></li>
  <li><p>I have seen following example:</p>
  
  <pre><code>@RequestMapping(value = "/form", method = RequestMethod.POST)
  public String handleFormUpload(@RequestParam("file") MultipartFile file) {
  
      if (!file.isEmpty()) {
          byte[] bytes = file.getBytes();
          // store the bytes somewhere
         return "redirect:uploadSuccess";
      } else {
          return "redirect:uploadFailure";
      }
  }
  </code></pre>
  
  <p>Originally I tried to follow this example but was always getting an error as it couldn't find this request param. So, in my controller I have done the following:</p>
  
  <pre><code>@RequestMapping(value = "/upload", method = RequestMethod.POST)
  public @ResponseBody
  ExtResponse upload(HttpServletRequest request, HttpServletResponse response)
  {
     // Create a JSON response object.
     ExtResponse extResponse = new ExtResponse();
     try {
         if (request instanceof MultipartHttpServletRequest)
         {
             MultipartHttpServletRequest multipartRequest =
                          (MultipartHttpServletRequest) request;
             MultipartFile file = multipartRequest.getFiles("file");
             InputStream input = file.getInputStream();
             // do the input processing
             extResponse.setSuccess(true);
          }
      } catch (Exception e) {
          extResponse.setSuccess(false);
          extResponse.setMessage(e.getMessage());
      }
      return extResponse;
  }
  </code></pre></li>
  </ol>
  
  <p>and it is working. If someone can tell me why @RequestParam did not work for me, I will appreciate. BTW I do have </p>
  
  <pre><code>    &lt;bean id="multipartResolver"
          class="org.springframework.web.multipart.commons.CommonsMultipartResolver"&gt;
          &lt;property name="maxUploadSize" value="2097152"/&gt;
      &lt;/bean&gt;
  </code></pre>
  
  <p>in my servlet context file.</p>