title: How to upload multipart file without storing on server harddisk
body: <p>My SpringBoot API receives multipart file. Then using RestTemplate I am sending same to different API. It works fine!!!</p>
  <p>Problem here is its storing multipart file on server harddisk. My API should  just <strong>act as an intermediate service</strong> between frontend and another API endpoint.</p>
  <p><strong>I do not want to store received file locally</strong>
  One thing I can do is delete after uploaded to different API.</p>
  <p>Is there any better way we do same thing i.e. without storing on server or automatic delete from server.</p>
  <p>My code is as below
  Controller</p>
  <pre><code>    @PostMapping(value = &quot;/uploadFile&quot;, consumes = { &quot;multipart/form-data&quot; })
      public SomePojoObject uploadFile(@RequestParam(&quot;file&quot;) MultipartFile mFile,
              @RequestParam(&quot;metaData&quot;) String metaData) {
  
          return service.uploadFile(mFile, metaData);
  
      }
  </code></pre>
  <p>And Service code is as below</p>
  <pre><code>private SomePojoObject uploadFile(MultipartFile mFile, String metaData) {
  
      File file = convertFile(mFile);
  
      LinkedMultiValueMap&lt;String, Object&gt; map = new LinkedMultiValueMap&lt;&gt;();
      map.add(&quot;FILE&quot;, new FileSystemResource(file));
      map.add(&quot;metaData&quot;, metaData);
          
      // Some more logic of resttemplate to upload document to server
  }
  
  
  public File convertFile(MultipartFile mFile) {
          File file = new File(mFile.getOriginalFilename());
          try {
              file.createNewFile();
              FileOutputStream fileOutputStream = new FileOutputStream(file);
              fileOutputStream.write(mFile.getBytes());
              fileOutputStream.close();
          } catch (IOException e) {
              e.printstackTrace();
          }
  
          return file;
      }
  
  </code></pre>