2

I'm using the Java Servlet 3.0 to upload files, using the @MultipartConfig annotation and request.getParts() to obtain the files.

When a file is uploaded, a TMP file is created in the Web Application work directory (tomcat/work/Catalina/localhost/webappname). For example:

upload_7c59101b_9f97_4e3f_9fa5_e484056d26fa_00000209.tmp

The application copies the file to another directory on the server - I'm doing this using the part.write() method but it's also working by obtaining the input stream and writing the bytes. Either way works fine.

I need to remove the TMP files after the upload, but I'm having trouble doing so. The part.delete() method doesn't do anything. I've also tried accessing the files in the directory using javax.servlet.context.tempdir and iterating over them to delete, but when calling a delete method, it always returns false. Using the Files.delete(path) method from Files.nio 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.

Does anyone have any other solutions to remove these TMP files? It's worth pointing out that I've tried using a HttpRequestListener too, but still cannot delete the files.

Many thanks

2
  • You need to close all streams to files you use. Commented Jul 31, 2015 at 9:37
  • Yes, all streams are being closed. Thanks. Commented Jul 31, 2015 at 10:04

3 Answers 3

2

You should (must!) not manipulate the files directly, you should use the getInputStream() method of the particular Part to get the content of the uploaded file. The servlet container (Tomcat in your case) will - or at least should - take care of the temporary files.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the suggestion. I've used part.getInputStream() rather than part.write(). Closing the input stream after writing it to a file seems to have resolved the issue and the temporary files are being removed.
This is not correct. The Part interface provides a write() method that allows a caller to move the underlying file to another location.
0

Along with InputStream.close(), use Part.delete() to remove the stored temporary file under work directory. Please refer the javadoc: Part.delete().

Comments

0

I agree with Jozef Chocholacek answer, simple solution CLOSE the input. We were using MultiPart messages with files upload. Since we were not closing the inputStream the files were stored there for a loooong time. They were deleted only on server restart.

After slightly changing the implementation with always closing the input part at the end.

Use try-> catch-> finally and put closing in finally part which will be called always even when the call of method fails.

The server is not storing .tmp files anymore.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.