美文网首页
递归删除FTP指定日期之前的文件

递归删除FTP指定日期之前的文件

作者: 二枚目 | 来源:发表于2021-04-23 16:34 被阅读0次

日常跑数的文件存放于FTP服务器需要定期删除;写一个删除FTP指定路径指定日期之前文件的工具类,后续或可应用于定时任务调度。
使用到的jar包:commons-net-3.8.0.jar
入参:FTP指定文件夹路径;指定日期时间
代码:

package com.example.demo.controller;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

@RestController
public class FTPFileClean {

    private Logger logger= LoggerFactory.getLogger(getClass());

    private FTPClient ftp;
    private Date date;

    @RequestMapping("/FTPFileClean")
    public String clean(String vFolder, String vDate) {
        // 获取FTP参数
        String ftpIP = "192.168.0.21";
        String ftpUserName = "username";
        String ftpPassword = "password";
        int ftpPort = 21;
        this.ftp = new FTPClient();
        this.ftp.setControlEncoding("UTF-8");
        try {
            int reply;
            this.ftp.connect(ftpIP, ftpPort);
            this.ftp.login(ftpUserName, ftpPassword);
            reply = this.ftp.getReplyCode();
            if (!FTPReply.isPositiveCompletion(reply)) {
                this.ftp.disconnect();
                return "FTP登录失败";
            }
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            date = sdf.parse(vDate);
            //递归删除该文件下过期文件
            deleteFile(vFolder);

            ftp.logout();
            return "成功";
        }
        catch (Exception e) {
            return "删除文件失败!" + e.getMessage();
        }
        finally {
            if (ftp.isConnected()) {
                try {
                    ftp.disconnect();
                } catch (IOException ioe) {

                }
            }
        }
    }

    /**
     * 递归删除文件
     * @param vFolder
     * @throws IOException
     */
    private void deleteFile(String vFolder) throws IOException {
        if (vFolder.startsWith("/") && vFolder.endsWith("/")) {
            FTPFile[] files = null;
            this.ftp.changeWorkingDirectory(vFolder);
            //需要设置一下模式才能获取子文件列表
            this.ftp.enterLocalPassiveMode();
            files = this.ftp.listFiles();
            for (FTPFile file : files) {
                if (file.isFile()) {
                    // 判断是否在指定日期之前
                    if (new Date(file.getTimestamp().getTimeInMillis()).before(date)) {
                        //需要再设置一下ftp路径才能删除
                        this.ftp.changeWorkingDirectory(vFolder);
                        this.ftp.deleteFile(file.getName());
                        System.out.println("删除文件:"+file.getName());
                    }
                }
                else if (file.isDirectory()) {
                    // 需要判断是否为该目录(./)及上级目录(../)。否则纳入递归会陷死循环
                    if (!".".equals(file.getName()) && !"..".equals(file.getName())) {
                        deleteFile(vFolder + file.getName() + "/");
                    }
                }
            }
        }
    }
}

相关文章

网友评论

      本文标题:递归删除FTP指定日期之前的文件

      本文链接:https://www.haomeiwen.com/subject/mewcrltx.html