Commit 0a1a0e7e authored by 杨郁彬's avatar 杨郁彬

新增删除明文文件目录的接口及定时任务删除超过1年的明文文件目录

parent 493a25b0
package com.cesgroup.bdc.cis.controller; package com.cesgroup.bdc.cis.controller;
import cn.hutool.core.io.FileUtil;
import com.cesgroup.bdc.cis.service.CisService; import com.cesgroup.bdc.cis.service.CisService;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
...@@ -13,6 +17,7 @@ import org.springframework.web.bind.annotation.ResponseBody; ...@@ -13,6 +17,7 @@ import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.HandlerMapping; import org.springframework.web.servlet.HandlerMapping;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import java.io.File; import java.io.File;
import java.time.YearMonth;
import java.util.*; import java.util.*;
/** /**
...@@ -25,6 +30,7 @@ import java.util.*; ...@@ -25,6 +30,7 @@ import java.util.*;
@Controller @Controller
@RequestMapping("/cis") @RequestMapping("/cis")
public class CisController { public class CisController {
private Log logger = LogFactory.getLog(getClass());
@Autowired @Autowired
private CisService cisService; private CisService cisService;
...@@ -146,4 +152,103 @@ public class CisController { ...@@ -146,4 +152,103 @@ public class CisController {
} }
} }
} }
/**
* 删除明文文件
*
* @param dir 明文文件路径 多个用-分隔
* @param request
* @return
*/
@RequestMapping("/removePlainFiles/{dir}/**")
@ResponseBody
public Map<String, String> removePlainFiles(@PathVariable String dir, HttpServletRequest request) {
Map<String, String> mm = new HashMap<>();
String path =
request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE).toString();
path = path.replace("/cis/removePlainFiles/", "");
path = path.replace("\\", "/");
String[] filePaths = path.split("-");
List<String> failFiles = new ArrayList<>();
for (int i = 0; i < filePaths.length; i++) {
StringBuffer filePath = new StringBuffer("");
filePath.append("fileBasePath").append(filePaths[i]);
this.recursiveRemove(filePath.toString(), failFiles);
}
if (failFiles.size() > 0) {
mm.put("status", "fail");
ObjectMapper objectMapper = new ObjectMapper();
String jsonString = null;
try {
jsonString = objectMapper.writeValueAsString(failFiles);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
mm.put("failFiles", jsonString);
} else {
mm.put("status", "success");
}
return mm;
}
/**
* 删除文件目录
*
* @param filePath
* @param failFiles
*/
private void recursiveRemove(String filePath, List<String> failFiles) {
boolean success = FileUtil.del(filePath);
if (!success) {
failFiles.add(filePath);
}
}
/**
* 清理不是最近一年的明文文件目录
*/
@Scheduled(cron = "00 00 22 1 * ?") //每月1号22:00:00执行
public void removeUnLastYearPlainFileDir() {
logger.info("开始清理不是最近一年的明文文件目录");
List<String> lastYearDirs = new ArrayList<>();
YearMonth currentYearMonth = YearMonth.now();
YearMonth previousYear = currentYearMonth.minusYears(1);
for (int i = 1; i < 13; i++) {
YearMonth yearMonth = previousYear.plusMonths(i);
int month = yearMonth.getMonth().getValue();
StringBuffer sb = new StringBuffer();
sb.append(yearMonth.getYear());
if (month >= 1 && month <= 9) {
sb.append("0").append(month);
} else {
sb.append(month);
}
lastYearDirs.add(sb.toString());
}
List<String> needRemoveDirs = new ArrayList<>();
File rootPath = new File(fileBasePath);
File[] listFiles = rootPath.listFiles();
for(int i=0;i<listFiles.length;i++){
if(listFiles[i].isDirectory()){
String dirName = listFiles[i].getName();
if(dirName.matches("\\d{6}") && !lastYearDirs.contains(dirName)){
StringBuffer filePath = new StringBuffer();
filePath.append(fileBasePath).append(dirName);
needRemoveDirs.add(filePath.toString());
}
}
}
List<String> failFiles = new ArrayList<>();
for (int i = 0; i < needRemoveDirs.size(); i++) {
this.recursiveRemove(needRemoveDirs.get(i), failFiles);
}
if (failFiles.size() > 0) {
for (int i = 0; i < failFiles.size(); i++) {
logger.error("目录[" + failFiles.get(i)+"]"+"删除失败");
}
}
logger.info("结束清理不是最近一年的明文文件目录");
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment