package com.wiscom.ism.webapi.ismController; import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationRunner; import org.springframework.stereotype.Component; import org.springframework.stereotype.Service; import java.io.File; import java.util.Calendar; import java.util.Date; import java.util.Timer; import java.util.TimerTask; @Component public class DeleteImgController implements ApplicationRunner { /* * 定时删除以图搜图上传到static upload images search 文件夹下的照片 * d定时每天的23点59分删除 * */ //项目启动后执行的方法 @Override public void run(ApplicationArguments args) throws Exception { startDeleteImgService();// } //定时器 public void startDeleteImgService() { System.out.println("启动删除===="); Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.HOUR_OF_DAY, 23); calendar.set(Calendar.MINUTE, 59); calendar.set(Calendar.SECOND, 0); Date time = calendar.getTime(); Timer timer = new Timer(); timer.scheduleAtFixedRate(new TimerTask() { public void run() { File file = new File("static/upload/images/search"); if (file.exists()) { String[] list = file.list(); for (String name : list) { File f = new File("static/upload/images/search", name); if (f.isFile()) { f.delete(); } } } } }, time, 1000 * 60 * 60 * 24); } //执行时间 间隔时间 }
原文地址:https://www.cnblogs.com/maocai2018/p/10122291.html
时间: 2024-10-11 10:59:03