title: Java Servlet 3.0 File Upload - Removing TMP Files
body: <p>I'm using the Java Servlet 3.0 to upload files, using the <code>@MultipartConfig</code> annotation and <code>request.getParts()</code> to obtain the files.</p>
  
  <p>When a file is uploaded, a TMP file is created in the Web Application work directory (<code>tomcat/work/Catalina/localhost/webappname</code>). For example:</p>
  
  <pre><code>upload_7c59101b_9f97_4e3f_9fa5_e484056d26fa_00000209.tmp
  </code></pre>
  
  <p>The application copies the file to another directory on the server - I'm doing this using the <code>part.write()</code> method but it's also working by obtaining the input stream and writing the bytes. Either way works fine.</p>
  
  <p>I need to remove the TMP files after the upload, but I'm having trouble doing so. The <code>part.delete()</code> method doesn't do anything. I've also tried accessing the files in the directory using <code>javax.servlet.context.tempdir</code> and iterating over them to delete, but when calling a delete method, it always returns false. Using the <code>Files.delete(path)</code> method from <code>Files.nio</code> returns an exception which claims the file is in use by another program (i.e. locked) and therefore cannot be deleted. The server is running Windows Server 2012 R2.</p>
  
  <p>Does anyone have any other solutions to remove these TMP files? It's worth pointing out that I've tried using a <code>HttpRequestListener</code> too, but still cannot delete the files.</p>
  
  <p>Many thanks</p>