代码:
import java.io.File;
import java.io.FileFilter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
public class DeleteFilesServlet extends HttpServlet {
public void destroy() {
super.destroy();
}
public void init() throws ServletException {
/*
* //方法一,在tomcat中"."是bin目录 File file=new File("."); File[]
* subFiles=file.listFiles();
*
* for(File f:subFiles){ //获取以xls为文件结尾的文件 if
* (f.getName().endsWith("xls")) { f.delete(); } }
*/
// 方法二
File file = new File(".");
File[] subFiles = file.listFiles(new FileFilter() {
public boolean accept(File pathname) {
if (pathname.getName().endsWith("xls")) {
return true;
}
return false;
}
});
for (File f : subFiles) {
f.delete();
}
}
}
web.xml:
//因为不需要用户访问,因此只需要配置以下文件即可,设置启动顺序为8
<servlet>
<servlet-name>DeleteFilesServlet</servlet-name>
<servlet-class>com.zpj.servlet.DeleteFilesServlet</servlet-class>
<load-on-startup>8</load-on-startup>
</servlet>