title: Spring MVC upload file to server directory
body: <p>I'm developing a web which have an upload PDF files feature. But I'm having an error. </p>
  
  <p>Here's what ive done so far:</p>
  
  <p>Multipart resolver:</p>
  
  <pre><code>&lt;bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"&gt;
      &lt;!-- one of the properties available; the maximum file size in bytes --&gt;
      &lt;property name="maxUploadSize" value="10000000"/&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>form that will upload:</p>
  
  <pre><code>&lt;form:form commandName="fileUpload" action="../admin/uploadPDF.do" method="post"  enctype="multipart/form-data"&gt;
       &lt;form:label path="fileData"&gt;Upload a File&lt;/form:label&gt; &lt;br /&gt;
       &lt;form:input type="file"  path="fileData" /&gt;
       &lt;input type="submit" value="upload" &gt;
  &lt;/form:form&gt;
  </code></pre>
  
  <p>Controller that catch the request the first time the user come to upload page, <code>AdminController.java</code></p>
  
  <pre><code>@RequestMapping( value = "/admin/module", method = RequestMethod.GET )
      public String student( @RequestParam( defaultValue = "" )
      String message, @RequestParam( defaultValue = "" )
      String messageType, HttpServletRequest request, ModelMap model )
      {
          model.addAttribute( "message", message );
          model.addAttribute( "messageType", messageType );
          model.addAttribute( new UploadItemBean() );
          HttpSession session = request.getSession();
          String returnVal = Credentials.checkSession( session );
  
          if( returnVal != null )
          {
              return returnVal;
          }
  
          return "als-student/module";
      }
  </code></pre>
  
  <p>Controller that will catch the request when upload file is submitted, <code>UploadController.java</code></p>
  
  <pre><code>@RequestMapping( value = "*/uploadPDF", method = RequestMethod.POST )
  public String getPDF( @RequestParam( defaultValue = "" )
  String message, @RequestParam( defaultValue = "" )
  String messageType, @RequestParam( "name" )
  String name, @RequestParam( "file" )
  MultipartFile file, HttpServletRequest request, ModelMap model )
  {
      ...
    if( !file.isEmpty() )
      {
          try
          {
              byte[] bytes = file.getBytes();
              System.out.println( bytes + ", " + name );
          }
          catch( IOException e )
          {
  
              e.printStackTrace();
          }
      }
      return "als-student/module";
  } 
  </code></pre>
  
  <p>Stacktrace:</p>
  
  <pre><code>Neither BindingResult nor plain target object for bean name 'fileUpload' available as request attribute
      at org.springframework.web.servlet.support.BindStatus.&lt;init&gt;(BindStatus.java:141)
      at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getBindStatus(AbstractDataBoundFormElementTag.java:168)
      at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPath(AbstractDataBoundFormElementTag.java:188)
      at org.springframework.web.servlet.tags.form.LabelTag.autogenerateFor(LabelTag.java:130)
      at org.springframework.web.servlet.tags.form.LabelTag.resolveFor(LabelTag.java:120)
      at org.springframework.web.servlet.tags.form.LabelTag.writeTagContent(LabelTag.java:90)
  ...
  ...
  </code></pre>
  
  <p>I also want to know how can I send the bean <code>fileUpload</code> to the form, because it seems that Its the one causing the error. And I also after uploading the file, how can I handle it to save to a folder in apache server ( If this is the good practice ) ?</p>