在用户注销时,确保您已关闭文件并检查File#delete()
返回的内容,然后再尝试删除它。
@Test
public void createTempFile() throws IOException {
File tf = File.createTempFile("hello", ".tmp", new File("."));
FileOutputStream fos = new FileOutputStream(tf);
fos.write("Hello, Temp!".getBytes());
Assert.assertTrue(tf.delete());
}
对比。
@Test
public void createTempFile() throws IOException {
File tf = File.createTempFile("hello", ".tmp", new File("."));
FileOutputStream fos = new FileOutputStream(tf);
fos.write("Hello, Temp!".getBytes());
fos.close();
Assert.assertTrue(tf.delete());
}
使用File#deleteOnExit(),文件将在虚拟机退出时被删除,这会在Tomcat关闭时发生。因此,它对用户注销没有帮助。
编辑
确保每个用户只有一个文件,并且跨多个请求。建议使用像Joop建议的
SessionListener
,在
HttpSessionListener#sessionCreated被调用时创建文件,并使用已知的键将其放入会话中,您可以使用
HttpSessionEvent#getSession()获取会话对象。当您注销时调用
HttpSession.#invalidate(),Listner方法
HttpSessionListener#sessionDestroyed将被调用,然后您可以从会话中获取文件并将其删除。
简单示例(仅使用doGet且没有SessionListener)
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@WebServlet(urlPatterns = "/ptf.html")
public class PopulateTempFile extends HttpServlet {
private static final long serialVersionUID = -144949663400032218L;
private static class TempFilePopulator {
private File tf = null;
public TempFilePopulator(String rootDir) throws IOException {
tf = File.createTempFile("hello", ".tmp", new File(rootDir));
}
public void populate(String line) throws IOException {
FileWriter fw = new FileWriter(tf, true);
fw.write(line + "\n");
fw.close();
}
public List<String> getContent() throws IOException {
List<String> lines = new ArrayList<String>();
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(tf)));
String line;
while(null != (line = br.readLine())) {
lines.add(line);
}
br.close();
return lines;
}
public boolean deleteTempFile() { return tf.delete(); }
public String toString() { return tf.getAbsolutePath(); }
}
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
HttpSession session = request.getSession();
TempFilePopulator tfp = (TempFilePopulator) session.getAttribute("tempfilepopulator");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<a href=\"" + request.getServletContext().getContextPath()
+ request.getServletPath() + "\">Refresh</a>");
out.println(" | ");
out.println("<a href=\"" + request.getServletContext().getContextPath()
+ request.getServletPath() + "?logout=true\">Logout</a>");
String logout = request.getParameter("logout");
if("true".equals(logout)) {
if(tfp != null) {
if(tfp.deleteTempFile()) {
log("Temp file '" + tfp + "' deleted.");
} else {
log("Unable to delete temp file '" + tfp + "'");
}
}
session.invalidate();
} else {
if(tfp == null) {
tfp = new TempFilePopulator(request.getServletContext().getRealPath("/"));
log("Temp file '" + tfp + "' created.");
session.setAttribute("tempfilepopulator", tfp);
}
tfp.populate(new Date().toString());
out.println("<p>Content of temp file</p>");
List<String> lines = tfp.getContent();
for(String line : lines) {
out.write(line);
out.write("<br/>");
}
}
out.println("</html>");
}
}
Created file: F:\workspace\eclipse\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps\JavaTestServlet\tmp\241B5CA17233009D92AAA46E1E378BAE.html
然后,在删除文件后,我检查file.exists();
,结果为:F:\workspace\eclipse\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps\JavaTestServlet\tmp\241B5CA17233009D92AAA46E1E378BAE.html exists: false
但是该文件可以从文件浏览器中看到... - Iordan Iordanov