java定时器删除服务器文件

java定时器删除服务器文件

2023年7月21日发(作者:)

java定时器删除服务器⽂件实现思路:1、配置⼀个监听器2、在监听器上配置⼀个定时器,⽤来执⾏定时任务3、在任务管理器配置任务Listener监听器:package er;import ption;import tream;import ;import ties;import ;import ask;import tContext;import tContextEvent;import tContextListener;//定时删除断点续传的附件public class ExcelTempFileListener implements ServletContextListener { private Timer timer; private SystemTaskTest systemTask; private static String every_time_run; static { Properties prop = new Properties(); InputStream inStrem = ssLoader() .getResourceAsStream("ties"); try { (inStrem); every_time_run = perty("every_time_run");

} catch (IOException e) { tackTrace(); } finally { try { (); } catch (IOException e) { tackTrace(); } } }

// 监听器初始⽅法 public void contextInitialized(ServletContextEvent sce) {

timer = new Timer(); systemTask = new SystemTaskTest(vletContext() .getRealPath("/"), vletContext()); try { vletContext().log("定时器已启动"); // 监听器获取⽹站的根⽬录 Long time = ong(every_time_run) * 1000 * 60 ;// 循环执⾏的时间 // 第⼀个参数是要运⾏的代码,第⼆个参数是从什么时候开始运⾏,第三个参数是每隔多久在运⾏⼀次。重复执⾏ le(systemTask, 10000, time); vletContext().log("已经添加任务调度表"); } catch (Exception e) { } } }

public void contextDestroyed(ServletContextEvent sce) { try { (); } catch (Exception e) { } }}

/** * 时间任务器 *

* @author xdb */class SystemTaskTest extends TimerTask { private ServletContext context; private String path; public SystemTaskTest(String path, ServletContext context) { = path; t = context; }

/** * 把要定时执⾏的任务就在run中 */ public void run() {

ExcelTempFileManager etf;

try { ("开始执⾏任务!"); // 需要执⾏的代码 etf = new ExcelTempFileManager(path); ();

("指定任务执⾏完成!"); } catch (Exception e) { tackTrace(); } }}任务管理类:package er;import ;import ption;import tream;import ties;public class ExcelTempFileManager implements Runnable { private static String path;// 路径

/** * 构造函数。初始化参数 *

* @param path */ public ExcelTempFileManager(String path) { = path; } /** * 把线程要执⾏的代码放在run()中 * 把线程要执⾏的代码放在run()中 */ public void run() { path = path + "upload"; File file = new File(path); deletefiles(file); } /** * 批量删除⽂件 *

* @param folder */ public void deletefiles(File folder) { File[] files = les(); for (int i = 0; i < ; i++) { deleteFolder(files[i]); } } /** * 删除⽂件 *

* @param file */ private void deleteFile(File file) { try { if (()) { // 删除符合条件的⽂件 (); }

} catch (Exception e) { tackTrace(); } } /** * 删除⽂件夹 *

* @param folder */ public void deleteFolder(File folder) { if (ctory()) { File[] files = les(); for (int i = 0; i < ; i++) { deleteFolder(files[i]); } // ⾮当前⽬录,删除 if (!olutePath().equalsIgnoreCase(path)) { (); } } else { deleteFile(folder); } }}中配置任务监听器

empFileListener ties配置⽂件 :#配置執⾏的时间,以分钟为单位:1440(60*24)为⼀天every_time_run=1440

发布者:admin,转转请注明出处:http://www.yc00.com/web/1689930309a295226.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信