?? 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 + -