whycxzp
2023-03-01 93a16e3707b870856594e89b950174b64bb7d6cd
src/main/java/com/whyc/service/FtpService.java
@@ -4,31 +4,47 @@
import com.whyc.dto.FileDirPath;
import com.whyc.dto.FtpHelper;
import com.whyc.dto.ZipUtils;
import org.springframework.beans.factory.annotation.Autowired;
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.util.Date;
import java.util.List;
@Service
@EnableScheduling   // 2.开启定时任务
public class FtpService {
    @Autowired(required = false)
    private FtpHelper ftpHelper;
    //定时上传指定目录下文件
    @Scheduled(cron = "0/20 06 17 * * ?")
    @Scheduled(cron = "59 59 23 ? * FRI")
    private void sendFtpFile(){
        FtpHelper ftp = new FtpHelper(YamlProperties.ftpIp, YamlProperties.ftpPort, YamlProperties.ftpUserName, YamlProperties.ftpPassword);
        String fileDirName = FileDirPath.getFileDirName();
        String root=fileDirName+File.separator+"face";
        String rootFace=fileDirName+File.separator+"face";
        String rootDoc=fileDirName+File.separator+"doc_file";
        String timeStr= ActionUtil.sdfwithFTP.format(new Date());
        try {
            FileOutputStream fos = new FileOutputStream(new File(root+".zip"));
            ZipUtils.toZip(root, fos,true);
            File file = new File(root+".zip");
            ftp.uploadFile(file, "face.zip");
            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);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }