lxw
2023-11-30 594a88ca5bee4d0f876e2759341db61b554a6d9a
src/main/java/com/whyc/service/FtpService.java
@@ -1,53 +1,99 @@
package com.whyc.service;
import com.enterprisedt.net.ftp.FTPException;
import com.whyc.constant.YamlProperties;
import com.whyc.dto.FileDirPath;
import com.whyc.dto.FtpHelper;
import com.whyc.dto.ZipUtils;
import com.whyc.util.ActionUtil;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.ParseException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.stream.Collectors;
@ConditionalOnProperty(prefix = "configFile",name = "type",havingValue = "2")
@Service
@EnableScheduling   // 2.开启定时任务
public class FtpService {
    //定时上传指定目录下文件
    //定时上传指定目录下文件,每周五凌晨开启备份
    @Scheduled(cron = "59 59 23 ? * FRI")
    private void sendFtpFile(){
        FtpHelper ftp = new FtpHelper(YamlProperties.ftpIp, YamlProperties.ftpPort, YamlProperties.ftpUserName, YamlProperties.ftpPassword);
    //@Scheduled(cron = "0/10 * * * * ?")
    private void sendFtpFile() throws IOException, FTPException, InterruptedException, ParseException {
        //先连接ftp服务器,获取备份目录,保留3次.这里是从2023年4月开始进行保留
        FtpHelper ftpRemote = new FtpHelper(YamlProperties.ftpIp, YamlProperties.ftpPort, YamlProperties.ftpUserName, YamlProperties.ftpPassword);
        String[] dirList = ftpRemote.getDirList();
        List<String> dirList2 = Arrays.asList(dirList);
        dirList2 = dirList2.stream().filter(item->Integer.parseInt(item.substring(0,4))>=2023 && Integer.parseInt(item.substring(5,7))>=4).collect(Collectors.toList());
        for (int i = 0; i < dirList2.size()-3; i++) {
            ftpRemote.deleteDir(dirList2.get(i));
        }
        String fileDirName = FileDirPath.getFileDirName();
        String rootFace=fileDirName+File.separator+"face";
        String rootDoc=fileDirName+File.separator+"doc_file";
        String timeStr= ActionUtil.sdfwithFTP.format(new Date());
        ftpRemote.mkdir(timeStr);
        ftpRemote.disconnect();
        try {
            FileOutputStream forootFace = new FileOutputStream(new File(rootFace+".zip"));
            ZipUtils.toZip(rootFace, forootFace,true);
            File filerootFace = new File(rootFace+".zip");
            ftp.uploadFile(filerootFace, timeStr+"/face.zip");
            filerootFace.delete();
            /*FileOutputStream forootDoc = new FileOutputStream(new File(rootDoc+".zip"));
            ZipUtils.toZip(rootDoc, forootDoc,true);
            File filerootDoc = new File(rootDoc+".zip");
            ftp.uploadFile(filerootDoc, timeStr+"/doc_file.zip");
            filerootDoc.delete();*/
            List<File> list=ftp.getFileList(rootDoc);
            if(list!=null&&list.size()>0){
                for (File file:list) {
                    String name=file.getPath().substring(file.getPath().lastIndexOf("doc_file"));
                    String pathName=timeStr+"/"+name.replace("\\","/");
                    ftp.uploadFile(file, pathName);
                }
            File docFile = new File(rootDoc);
            File[] fileList = docFile.listFiles();
            List<File> list = Arrays.asList(fileList);
            for (File file:list) {
                //压缩传输
                File fileZip = new File(file.getAbsolutePath() + ".zip");
                ZipUtils.toZip(file.getAbsolutePath(),new FileOutputStream(fileZip),true);
                //System.out.println(fileZip.toString()+"压缩完毕");
            }
            //System.out.println("进行FTP传输中...");
            CountDownLatch latch = new CountDownLatch(list.size()+1);
            for (File file:list) {
                new Thread(){
                    @Override
                    public void run() {
                        //压缩传输
                        File fileZip = new File(file.getAbsolutePath() + ".zip");
                        //System.out.println("线程内 压缩后的目标路径为:"+fileZip.toString());
                        FtpHelper ftp = new FtpHelper(YamlProperties.ftpIp, YamlProperties.ftpPort, YamlProperties.ftpUserName, YamlProperties.ftpPassword);
                        try {
                            //ZipUtils.toZip(file.getAbsolutePath(),new FileOutputStream(fileZip),true);
                            String pathName=timeStr+"/"+file.getName()+".zip";
                            //System.out.println("传输目标路径确认为:"+pathName);
                            ftp.uploadFile2(fileZip, pathName);
                            //System.out.println(fileZip+"传输完毕");
                            fileZip.delete();
                            //System.out.println(fileZip+"传输中转压缩包删除完毕");
                            ftp.disconnect();
                            latch.countDown();
                        } catch (Exception e) {
                            e.printStackTrace();
                            fileZip.deleteOnExit();
                            ftp.disconnect();
                            latch.countDown();
                        }
                    }
                }.start();
            }
            latch.countDown();
            latch.await();
            //System.out.println("传输完毕");
        } catch (Exception e) {
            e.printStackTrace();
        }
        ftp.disconnect();
    }
}