定時清理磁盤

merge-requests/5/head
yiming 4 years ago
parent ef8cfff5b8
commit ccb509a47e

@ -27,6 +27,8 @@ public class ConfigProperties {
private Integer serverMode;
private Integer deleteFileDays;
private String userUrl;
private KSEC ksec;

@ -0,0 +1,69 @@
package com.zhehekeji.web.service;
import com.zhehekeji.web.config.ConfigProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
import java.time.*;
@Component
@EnableScheduling
@Slf4j
public class CronTab {
@Resource
private ConfigProperties configProperties;
private static LocalDateTime now;
@Scheduled(cron = "0 0 0 * * ?")
//@Scheduled(cron = "0 0/1 * * * *")
public void file() {
log.info(" corn delete file");
now = LocalDateTime.now();
File dir = new File(configProperties.getSavePath().getMediaPath());
checkFileTime(dir,configProperties.getDeleteFileDays());
File dir2 = new File(configProperties.getSavePath().getMp4Path());
checkFileTime(dir2,configProperties.getDeleteFileDays());
}
public static void checkFileTime(File dir,int days) {
if (dir.isDirectory()) {
String[] children = dir.list();
if(children.length == 0){
log.info("delete path:{}",dir.getPath());
dir.delete();
}
for (int i = 0; i < children.length; i++) {
checkFileTime(new File(dir, children[i]),days);
}
}else {
//判斷時間
BasicFileAttributes attr = null;
try {
Path path = dir.toPath();
attr = Files.readAttributes(path, BasicFileAttributes.class);
} catch (IOException e) {
e.printStackTrace();
}
// 创建时间
Instant instant = attr.creationTime().toInstant();
LocalDateTime createTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
Duration duration = Duration.between(createTime,now);
if(days<=duration.toDays()){
log.info("delete path:{}",dir.getPath());
dir.delete();
}
}
}
}

@ -69,4 +69,6 @@ scanCodeMode:
goodsCodeTypes:
- 14
trayCodeTypes:
- 14
- 14
# 照片 視頻保存多久
deleteFileDays: 365

@ -69,4 +69,6 @@ scanCodeMode:
goodsCodeTypes:
- 14
trayCodeTypes:
- 14
- 14
# 照片 視頻保存多久
deleteFileDays: 365

@ -69,4 +69,6 @@ scanCodeMode:
goodsCodeTypes:
- 14
trayCodeTypes:
- 14
- 14
# 照片 視頻保存多久
deleteFileDays: 365
Loading…
Cancel
Save