springboot定期清理文件

springboot定期清理文件

搬砖需求 今天在使用springboot清理文件 实现使用ThreadPoolTaskScheduler定时触发动态执行需要清理根据文件创建时间来清理指定多少天之前的文件仅供大家参考直接上代码

配置个ThreadPoolTaskScheduler bean

@Bean(“taskScheduler”)
public TaskScheduler taskScheduler() {
ThreadPoolTaskScheduler threadPoolTaskScheduler = new ThreadPoolTaskScheduler();
threadPoolTaskScheduler.setPoolSize(10);
threadPoolTaskScheduler.setWaitForTasksToCompleteOnShutdown(true);
return threadPoolTaskScheduler;
}

  1. 触发定时任务

package com.cre.seal.task;

import com.cre.seal.util.DeleteFilesUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

@Slf4j
@Component
public class SchedulingTask {

private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@Value("${templateExport.Path}")
private String attachPath;@Scheduled(cron = "${time.cron}")
@Async("taskScheduler")
public void checkClenFile() {log.info("======定时清理文件任务开始于:{}", sdf.format(new Date()));String filePath = attachPath;// 删除多少天之前文件int delCount = DeleteFilesUtils.moveFileToReady(filePath, 7);if (delCount > 0) {log.info("======本次从:{}" + filePath + "下清理" + delCount + "份文件");} else {log.info("======暂时没有要清理的文件");}log.info("======定时清理文件任务结束于:{}", sdf.format(new Date()));
}

}

清理文件工具

package com.cre.seal.util;

import java.io.File;
import java.util.Calendar;
import java.util.Date;

public class DeleteFilesUtils {

public static Integer moveFileToReady(String fromDir,int howDays ) {File srcDir = new File(fromDir);if (!srcDir.exists()) {return 0;}File[] files = srcDir.listFiles();if (files == null || files.length <= 0) {return 0;}// 删除文件总数int delTotal = 0;Date today = new Date();for (int i = 0; i < files.length; i++) {if (files[i].isFile()) {try {File ff = files[i];long time = ff.lastModified();Calendar cal = Calendar.getInstance();cal.setTimeInMillis(time);Date lastModified = cal.getTime();//(int)(today.getTime() - lastModified.getTime())/86400000;long days = getDistDates(today, lastModified);// 删除多少天前之前文件if (days >= howDays) {files[i].delete();delTotal++;}} catch (Exception e) {e.printStackTrace();}}}return delTotal;
}/*** @param startDate* @param endDate* @return*/
public static long getDistDates(Date startDate, Date endDate) {long totalDate = 0;Calendar calendar = Calendar.getInstance();calendar.setTime(startDate);long timestart = calendar.getTimeInMillis();calendar.setTime(endDate);long timeend = calendar.getTimeInMillis();totalDate = Math.abs((timeend - timestart)) / (1000 * 60 * 60 * 24);return totalDate;
}

}

springboot定期清理文件相关推荐

  1. SpringBoot图文教程4—SpringBoot 实现文件上传下载(亲测)

    SpringBoot 图文教程系列文章目录 SpringBoot图文教程1「概念+案例 思维导图」「基础篇上」 SpringBoot图文教程2-日志的使用「logback」「log4j」 Spring ...

  2. springboot改文件头_SpringBoot图文教程4—SpringBoot 实现文件上传下载

    有天上飞的概念,就要有落地的实现 概念+代码实现是本文的特点,教程将涵盖完整的图文教程,代码案例 文章结尾配套自测面试题,学完技术自我测试更扎实 概念十遍不如代码一遍,朋友,希望你把文中所有的代码案例 ...

  3. Springboot实现文件上传,并防止同文件重复上传

    目录 主要流程 编写接受文件上传的Controller 编写文件操作结果类 编写文件操作类 知识总结 参考 主要流程 在配置文件中添加文件操作的配置,示例: storage:image:#保存位置sa ...

  4. Springboot 下载文件

    Springboot下载文件比较简单,以下是代码: 目录结构 FileUtil为文件工具类,里面包括下载的方法,以下为FileUtil的代码: public class FileUtil {publi ...

  5. SpringBoot集成文件 - 如何基于POI-tl和word模板导出庞大的Word文件?

    前文我们介绍了通过Apache POI通过来导出word的例子:那如果是word模板方式,有没有开源库通过模板方式导出word呢?poi-tl是一个基于Apache POI的Word模板引擎,也是一个 ...

  6. SpringBoot实现文件上传和下载

    文件上传需要使用到 MultipartResolver接口. Spring MVC 使用 MultipartResolver接口的实现类:CommonsMultipartResolver .Commo ...

  7. springboot yml文件不是绿叶子问题

    项目的yml文件一直不显示绿色小叶子,显示文本类型或者下图类型,项目启动运行不会影响,但是看着不爽,必需整它,终于,今天解决了. 先看一下我之前的yml格式 还有这样的,看起来不舒服. 解决方案 ct ...

  8. 基于springboot的文件上传功能的实现

    基于springboot的文件上传功能的实现: 前言:本人是刚学习java后端不久,所以通过记录一下平时所学知识,方便日后的复习,如果有出错的地方,还望包含. 1.使用的工具是idea,和spring ...

  9. 【SpringBoot学习】5、SpringBoot 实现文件上传,图片上传并显示功能

    SpringBoot 实现文件上传,图片上传并显示功能 我先看一下<颈椎病康复指南>再给大家说怎么实现的这两个功能,毕竟只是一个新手,解决这种复杂点的问题(相对而言),还是需要花费大量时间 ...

最新文章

  1. Python图像处理,cv2模块,OpenCV实现人脸检测蔡徐坤
  2. 学习VIM编辑器的使用
  3. python itertools模块位置_Python高效编程之itertools模块详解
  4. android PowerManage
  5. 计算机网络——因特网的接入技术
  6. 计算机基础知识二进制转换,计算机基础知识数制转换
  7. 通过负载均衡器+域名实现容灾切换-(8)基于DNS解析的GSLB在BS架构中应用实践(转)(2)...
  8. 怎么使用小爱同学音响_小爱同学语音唤醒功能怎么设置,小爱同学音箱的优缺点是什么...
  9. Python代码 52周存钱计划
  10. 苦心研究两周,我特么终于搞懂啥是「元宇宙」了
  11. 华为交换机根据已知一个IP查他对应的MAC地址和交换机端口命令
  12. 2021高考查询成绩公众号,微信怎么查高考成绩2021 微信高考成绩查询系统入口
  13. 黑马点评--优惠卷秒杀
  14. 谈谈对高内聚低耦合的认识
  15. Json对象转json数组
  16. 信息量理解、信息熵公式的推导
  17. 关于Discuz论坛整合问题
  18. 推荐一些非常好用的网盘搜索神器
  19. 英语语法学习--冠词
  20. 20145204张亚军——web安全基础实践

热门文章

  1. css实现固定的图片比例
  2. 微信退款服务器系统失败怎么办,微信退款多久到账?微信退款不成功怎么办?...
  3. 程序员:写作能收获什么?
  4. 网站开发之ie下在线浏览pdf文件无需本地支持
  5. MES管理系统打造家具数字工厂,实现家具企业互联网+
  6. 关于html中的图片插入
  7. 【OVS2.5.0源码分析】sFlow实现分析(3)
  8. python绝对方向角度值_哪个选项是turtle绘图中角度坐标系的绝对0度方向?_学小易找答案...
  9. onedrive电脑手机不同步_关于OneDrive,移动端同步以及显示不及时的问题。
  10. java arthas使用