lxw
2023-07-11 b7d5952f03167ee8acbd1c9c9e8f55fb8393367d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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.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.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.stream.Collectors;
 
@Service
@EnableScheduling   // 2.开启定时任务
public class FtpService {
    //定时上传指定目录下文件
    @Scheduled(cron = "59 59 23 ? * FRI")
    //@Scheduled(cron = "0/10 * * * * ?")
    private void sendFtpFile() throws IOException, FTPException, InterruptedException {
        //先连接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));
        }
        ftpRemote.disconnect();
 
 
        FtpHelper ftp = new FtpHelper(YamlProperties.ftpIp, YamlProperties.ftpPort, YamlProperties.ftpUserName, YamlProperties.ftpPassword);
        String fileDirName = FileDirPath.getFileDirName();
        String rootFace=fileDirName+File.separator+"face";
        String rootDoc=fileDirName+File.separator+"doc_file";
        String timeStr= ActionUtil.sdfwithFTP.format(new Date());
        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();
            ftp.disconnect();
 
            /*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);
            File docFile = new File(rootDoc);
            //File docFile = new File("F:\\BaiduNetdiskDownload");
            File[] list = docFile.listFiles();
            CountDownLatch latch = new CountDownLatch(list.length+1);
            if(list!=null&&list.length>0){
                for (File file:list) {
                    new Thread(){
                        @Override
                        public void run() {
                            //压缩传输
                            File fileZip = new File(file.getAbsolutePath() + ".zip");
                            try {
                                ZipUtils.toZip(file.getAbsolutePath(),new FileOutputStream(fileZip),true);
                                String pathName=timeStr+"/doc_file/"+file.getName()+".zip";
                                FtpHelper ftp = new FtpHelper(YamlProperties.ftpIp, YamlProperties.ftpPort, YamlProperties.ftpUserName, YamlProperties.ftpPassword);
                                ftp.uploadFile(fileZip, pathName);
                                fileZip.delete();
                                ftp.disconnect();
                                latch.countDown();
                            } catch (Exception e) {
                                e.printStackTrace();
                                latch.countDown();
                            }
                        }
                    }.start();
 
                }
            }
            latch.countDown();
            latch.await();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}