title: How do I convert MultipartFile to required type byte[]
body: <p>I'm working with spring Data-Jpa using spring boot. I have a multipart form to upload file to the database with additional fields such as ( firstName, lastName, email, photo). But I keep getting this error message for converting multipart file into required byte[] type. How can I convert the image file from multipart file to byte[] type?</p>
  
  <p>Here is my ControllerClass:</p>
  
  <pre><code>    @PostMapping("/save")
  public String saveUserWithPhoto(@ModelAttribute("user") User user,
                           @RequestParam("file") MultipartFile file) throws IOException {
      String fileName = StringUtils.cleanPath(file.getOriginalFilename());
  
      if (!(fileName == null) || fileName.contains("..")) {       
          byte[] photoBytes = file.getBytes();     
          user.setPhoto(photoBytes);
          userService.saveUserWithPhoto(user);
          return "redirect:/profile";
      }
      .......
      return "redirect:/new-user";
  
  }
  </code></pre>
  
  <p>Here is my serviceClass:</p>
  
  <pre><code>  @Override
  public void saveUserWithPhoto(User theUser){
      userRepository.save(theUser);
  }
  </code></pre>
  
  <p>Here is my EntityClass:</p>
  
  <pre><code>@Entity
  @Table(name = "users")
  public class User{
  
      @Id
      @GeneratedValue
      private long id;
  
      @Column(name = "first_name")
      private String firstName;
  
     @Column(name = "last_name")
      private String lastName;
  
      @Column(name = "email")
      private String email;
  
      @Lob
      @Column(name="photo")
      private byte[] photo;
  
  }
  </code></pre>