title: Java spring: best way to convert a File to a MultipartFile
body: <p>I created a file (below) but I want to convert it to MultipartFile, how can I do it?</p>
  
  <p>I already tried this code, without sucess:</p>
  
  <pre><code>File file = new File("text.txt");
  
  FileInputStream input = new FileInputStream(file);
  MultipartFile multipartFile = new MockMultipartFile("file", file.getName(), "text/plain", IOUtils.toByteArray(input));
  </code></pre>
  
  <p>and that one result in a error:</p>
  
  <pre><code>File file = new File("text.txt");
      DiskFileItem fileItem = new DiskFileItem("file", "text/plain", false, file.getName(), (int) file.length() , file.getParentFile());
      fileItem.getOutputStream();
      MultipartFile multipartFile = new CommonsMultipartFile(fileItem);
  </code></pre>
  
  <p>Error</p>
  
  <blockquote>
    <p>CommonsMultipartFile (org.apache.commons.fileupload.FileItem) in
    CommonsMultipartFile cannot be applied to
    (org.apache.tomcat.util.http.fileupload.disk.DiskFileItem)</p>
  </blockquote>
  
  <p>Thanks</p>