title: How does one specify a temp directory for file uploads in Spring Boot?
body: <p>I'm using Spring Boot and need to let users upload files for processing. Right now, the file uploads to /home/username/git/myproject which is not great.</p>
  
  <p>How do I make Spring put those file uploads into a temporary directory that will be periodically purged by application restart (or some other means)? </p>
  
  <p>Here's what I've tried... but it doesn't work. File still saves to my working directory.</p>
  
  <pre><code>public class Application implements CommandLineRunner {
  
      /*
       * This doesn't seem to work.
       */
      @Bean
      MultipartConfigElement multipartConfigElement() {
          MultipartConfigFactory factory = new MultipartConfigFactory();
          factory.setMaxFileSize("128KB");
          factory.setMaxRequestSize("128KB");
          factory.setLocation(System.getProperty("java.io.tmpdir"));
          return factory.createMultipartConfig();
      }
  
  /* other stuff, main(), etc */
  
  }
  </code></pre>
  
  <p>PS I'm just running my app by executing Application and it's using embedded Tomcat.</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>Ok I've got it sorted out. I was converting the incoming MultipartFile to a normal File like so:</p>
  
  <pre><code>private File convertMultipartFileToFile(MultipartFile file) throws IOException
      {    
          File convFile = new File(file.getOriginalFilename());
          convFile.createNewFile(); 
          FileOutputStream fos = new FileOutputStream(convFile); 
          fos.write(file.getBytes());
          fos.close(); 
          return convFile;
      }
  </code></pre>
  
  <p>Instead, I should have been creating a new File in the designated temporary directory like this:</p>
  
  <pre><code>private File convertMultipartFileToFile(MultipartFile file) throws IOException
      {    
          File convFile = File.createTempFile("temp", ".xlsx"); // choose your own extension I guess? Filename accessible with convFile.getAbsolutePath()
          FileOutputStream fos = new FileOutputStream(convFile); 
          fos.write(file.getBytes());
          fos.close(); 
          return convFile;
      }
  </code></pre>
  
  <p>Now you may be asking, "Well what about the 'multipart.location' setting of the application.properties file?" That setting, obvious in retrospect, controls only where the ephemeral multipart file goes. If you <a href="https://unix.stackexchange.com/questions/24952/script-to-monitor-folder-for-new-files">watch that directory with a script</a>, you'll see that a 'upload_.tmp' file appears briefly and then disappears. 'multipart.location' has nothing to do with any persistent File objects you might create. </p>
  
  <p>(Note, you may be able to use the MultipartBean snippet from above instead of application.properties, but I didn't try it and why would you want to?)</p>
  
  <p>To change the value of your true temp directory, you can use "-Djava.io.tmp=/path/to/dir" VM argument to specify whatever you want before running your Spring Boot application.</p>