定時清理磁盤

taiwan-lingli-new
yiming 4 years ago
parent feece99ab5
commit 261284ded8

@ -24,11 +24,11 @@ public class CronTab {
private static LocalDateTime now; private static LocalDateTime now;
//@Scheduled(cron = "0 0 0 * * ?") @Scheduled(cron = "0 0 0 * * ?")
@Scheduled(cron = "0 0/1 * * * *") //@Scheduled(cron = "0 0/1 * * * *")
public void file() { public void file() {
log.info(" corn delete file");
now = LocalDateTime.now(); now = LocalDateTime.now();
log.info(" corn delete file ,time:{}",now);
File dir = new File(configProperties.getSavePath().getMediaPath()); File dir = new File(configProperties.getSavePath().getMediaPath());
checkFileTime(dir,configProperties.getDeleteFileDays()); checkFileTime(dir,configProperties.getDeleteFileDays());
File dir2 = new File(configProperties.getSavePath().getMp4Path()); File dir2 = new File(configProperties.getSavePath().getMp4Path());
@ -36,7 +36,9 @@ public class CronTab {
} }
public static void checkFileTime(File dir,int days) { public static void checkFileTime(File dir,int days) {
if(!dir.exists()){
return;
}
if (dir.isDirectory()) { if (dir.isDirectory()) {
String[] children = dir.list(); String[] children = dir.list();
if(children.length == 0){ if(children.length == 0){
@ -52,16 +54,19 @@ public class CronTab {
try { try {
Path path = dir.toPath(); Path path = dir.toPath();
attr = Files.readAttributes(path, BasicFileAttributes.class); attr = Files.readAttributes(path, BasicFileAttributes.class);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
// 创建时间 if(attr != null){
Instant instant = attr.creationTime().toInstant(); // 创建时间
LocalDateTime createTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault()); Instant instant = attr.creationTime().toInstant();
Duration duration = Duration.between(createTime,now); LocalDateTime createTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
if(days<=duration.toDays()){ Duration duration = Duration.between(createTime,now);
log.info("delete path:{}",dir.getPath()); if(days<=duration.toDays()){
dir.delete(); log.info("delete path:{}",dir.getPath());
dir.delete();
}
} }
} }
} }

Loading…
Cancel
Save