title: Uploading an image selected by the user into the database with Spring and Hibernate
body: <p>Please bear with me since I only learned to code a few months ago. I am having trouble uploading images selected by the user into a database. I am able to upload hard-coded images (e.g. "C:\imageName.jpg").</p>
  
  <p>My jsp code:</p>
  
  <pre><code>&lt;form:input path="pic" type="file" class="btn" name="image" enctype="multipart/form-data" /&gt;
  </code></pre>
  
  <p>EDIT</p>
  
  <p>Thank you M. Deinum, after reading the link you posted, I have added this bean:</p>
  
  <pre><code>&lt;bean id="multipartResolver"
          class="org.springframework.web.multipart.commons.CommonsMultipartResolver"&gt;
          &lt;property name="maxUploadSize" value="100000"/&gt;
  
  &lt;/bean&gt;
  </code></pre>
  
  <p>My controller now has this code:</p>
  
  <p>RequestMapping now contains this: <code>headers = "content-type=multipart/*"</code>
  Additional parameter: <code>MultipartHttpServletRequest request</code></p>
  
  <pre><code>@RequestParam(value="image", defaultValue="") MultipartFile file
  
  byte[] bytes = null;
  if (!file.isEmpty()) {
                  bytes = file.getBytes();
                  InputStream inputStream = file.getInputStream();
                  inputStream.read(bytes);
                  inputStream.close();
              }
  </code></pre>
  
  <p>My new error code:</p>
  
  <pre><code>The server refused this request because the request entity is in a format not supported by the requested resource for the requested method.
  </code></pre>
  
  <p>FURTHER EDIT</p>
  
  <p>For anyone interested this is the entire code of my controller:</p>
  
  <pre><code>@RequestMapping(value="/processsettings", method = RequestMethod.POST, headers = "content-type=multipart/*")
  public String processSettings(@ModelAttribute("changeUser") User changeUser, BindingResult result, HttpSession session, Model model, HttpServletResponse response, MultipartHttpServletRequest request,
  @RequestParam(value="image", defaultValue="") MultipartFile file) throws IOException, NoSuchAlgorithmException, InvalidKeySpecException {
  
      byte[] bytes = null;
  
      if (!file.isEmpty()){
          bytes = file.getBytes();
          InputStream inputStream = file.getInputStream();
          inputStream.read(bytes);
          inputStream.close();
      }
      changeUser.setPic(bytes);
      userService.updateUser(changeUser);
      session.setAttribute("currentUser", changeUser);
  
      return "Settings";
  }
  </code></pre>