lxw
2022-07-19 1896294cc19e59179309f6b53749a97d3cda0c59
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
package com.whyc.service;
 
import com.whyc.constant.YamlProperties;
import com.whyc.dto.FileDirPath;
import com.whyc.dto.FtpHelper;
import com.whyc.dto.ZipUtils;
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;
 
@Service
@EnableScheduling   // 2.开启定时任务
public class FtpService {
    //定时上传指定目录下文件
    @Scheduled(cron = "0/20 57 16 * * ?")
    private void sendFtpFile(){
        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";
        try {
            FileOutputStream forootFace = new FileOutputStream(new File(rootFace+".zip"));
            ZipUtils.toZip(rootFace, forootFace,true);
            File filerootFace = new File(rootFace+".zip");
            ftp.uploadFile(filerootFace, "face.zip");
 
            FileOutputStream forootDoc = new FileOutputStream(new File(rootDoc+".zip"));
            ZipUtils.toZip(rootDoc, forootDoc,true);
            File filerootDoc = new File(rootDoc+".zip");
            ftp.uploadFile(filerootDoc, "doc_file.zip");
        } catch (Exception e) {
            e.printStackTrace();
        }
        ftp.disconnect();
    }
}