📄 rubbishattachmentcleaner.java
字号:
package fengyun.Fastmail.servlets;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
/**
* 定时清除垃圾附件
* @author fengyun
* @version 1.00
*/
public class RubbishAttachmentCleaner extends HttpServlet implements Runnable {
private static String AttachmentTempPath = null; //附件临时路径
private static final long SleepTime = 3600000; //休眠时间间隔
private static Thread Cleaner = null; //清除文件的时间线程
/**
* 初始化Servlet
* @param config 设置信息
*/
public void init(ServletConfig config) throws ServletException
{
super.init(config);
AttachmentTempPath = config.getInitParameter("AttachmentTempPath");
Cleaner = new Thread(this);
Cleaner.start();
}
/**
* 清除附件的运行函数
*/
public void run() {
File file = new File(this.AttachmentTempPath);
if (!file.exists() || file.isFile()) return ;
while(true) {
String[] tmpFileList = file.list(new AttachmentFilenameFilter(this.SleepTime));
for(int i = 0; i < tmpFileList.length ; i++) {
File tmpFile = new File(this.AttachmentTempPath + File.separator + tmpFileList[i]);
if (tmpFile.exists() && tmpFile.isFile()) tmpFile.delete();
}
try {
Cleaner.sleep(SleepTime);
}
catch(Exception e) {
e.printStackTrace();
}
}
}
/**
* 选取过期附件的过滤器
*/
private static class AttachmentFilenameFilter implements FilenameFilter {
private long StayTime = 1200000;
/**
* 构造过滤器
* @param staytime 选取的时间间隔
*/
public AttachmentFilenameFilter(long staytime) {
this.StayTime = staytime;
} public boolean accept(File dir,String filename) { File file=new File(dir.getAbsolutePath() + File.separator + filename); if (file.isDirectory()) return false; int cur = filename.indexOf(";"); if (cur < 0 || cur >= filename.length()) return false;
long createtime = System.currentTimeMillis();
long thistime = createtime; try {
createtime = Long.valueOf(filename.substring(0,cur)).longValue(); } catch(Exception e) { createtime = thistime;
e.printStackTrace(); }
if (createtime < (thistime - this.StayTime)) return true; else return false;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -