确保在用户注销时尝试将其删除之前已关闭该文件并检查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()); // fails because the file could not be deleted
// and delete() returns false
}
对比
@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()); // passes, file deleted
}
使用File#deleteOnExit()文件将在 VM 退出时被删除,这会在您的 tomcat 关闭时发生。所以它对用户注销没有帮助。
编辑
确保每个用户和多个请求只有一个文件。我建议您SessionListener
按照 Joop 的建议使用,在调用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>");
}
}