Java Servlet 3.0 文件上传 - 删除 TMP 文件 [英] Java Servlet 3.0 File Upload - Removing TMP Files

查看:38
本文介绍了Java Servlet 3.0 文件上传 - 删除 TMP 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Java Servlet 3.0 上传文件,使用 @MultipartConfig 批注和 request.getParts() 获取文件.

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

上传文件时,会在 Web 应用程序工作目录 (tomcat/work/Catalina/localhost/webappname) 中创建 TMP 文件.例如:

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

应用程序将文件复制到服务器上的另一个目录 - 我使用 part.write() 方法执行此操作,但它也通过获取输入流并写入字节来工作.无论哪种方式都可以正常工作.

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.

我需要在上传后删除 TMP 文件,但我在这样做时遇到了问题.part.delete() 方法不做任何事情.我还尝试使用 javax.servlet.context.tempdir 访问目录中的文件并迭代它们以删除,但是在调用 delete 方法时,它总是返回 false.使用 Files.nio 中的 Files.delete(path) 方法会返回一个异常,该异常声明该文件正在被另一个程序使用(即锁定),因此无法删除.服务器运行的是 Windows Server 2012 R2.

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.

有没有人有任何其他解决方案来删除这些 TMP 文件?值得指出的是,我也尝试过使用 HttpRequestListener,但仍然无法删除文件.

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.

非常感谢

推荐答案

您应该(必须!)不要直接操作文件,您应该使用特定 getInputStream() 方法部分 获取上传文件的内容.servlet 容器(在您的情况下为 Tomcat)将 - 或者至少应该 - 处理临时文件.

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.

这篇关于Java Servlet 3.0 文件上传 - 删除 TMP 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆