| | |
| | | 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; |
| | | |
| | | @ConditionalOnProperty(prefix = "configFile",name = "type",havingValue = "2") |
| | | @Service |
| | | @EnableScheduling // 2.开启定时任务 |
| | | public class FtpService { |
| | | //定时上传指定目录下文件,每周五凌晨开启备份 |
| | | @Scheduled(cron = "59 59 23 ? * FRI") |
| | | //@Scheduled(cron = "0 29 15 * * ?") |
| | | public void sendFtpFile() throws IOException, FTPException, ParseException { |
| | | //先连接ftp服务器 |
| | | FtpHelper ftpRemote = new FtpHelper(YamlProperties.ftpIp, YamlProperties.ftpPort, YamlProperties.ftpUserName, YamlProperties.ftpPassword); |
| | | String fileDirName = FileDirPath.getFileDirName(); |
| | | String rootDoc=fileDirName+File.separator+"doc_file"; |
| | | String timeStr= ActionUtil.sdfwithFTP.format(new Date()); |
| | | ftpRemote.mkdir(timeStr); |
| | | //备份过程是否出现过问题,标识符 |
| | | boolean isError=false; |
| | | try { |
| | | |
| | | File docFile = new File(rootDoc); |
| | | File[] fileList = docFile.listFiles(); |
| | | //File[] fileList2 = new File[fileList.length]; |
| | | //BeanUtils.copyProperties(fileList2,fileList); |
| | | for (File file:fileList) { |
| | | //压缩传输 |
| | | File fileZip = new File(file.getAbsolutePath() + ".zip"); |
| | | ZipUtils.toZip(file.getAbsolutePath(),new FileOutputStream(fileZip),true); |
| | | //System.out.println(fileZip.toString()+"压缩完毕"); |
| | | //System.out.println("进行FTP传输中..."); |
| | | try { |
| | | //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; |
| | | // |
| | | //@ConditionalOnProperty(prefix = "configFile",name = "type",havingValue = "2") |
| | | //@Service |
| | | //@EnableScheduling // 2.开启定时任务 |
| | | //public class FtpService { |
| | | // //定时上传指定目录下文件,每周五凌晨开启备份 |
| | | // //@Scheduled(cron = "59 59 23 ? * FRI") |
| | | // @Scheduled(cron = "0 30 8 * * ?") |
| | | // public void sendFtpFile() throws IOException, FTPException, ParseException { |
| | | // //先连接ftp服务器 |
| | | // FtpHelper ftpRemote = new FtpHelper(YamlProperties.ftpIp, YamlProperties.ftpPort, YamlProperties.ftpUserName, YamlProperties.ftpPassword); |
| | | // String fileDirName = FileDirPath.getFileDirName(); |
| | | // String rootDoc=fileDirName+File.separator+"doc_file"; |
| | | // String timeStr= ActionUtil.sdfwithFTP.format(new Date()); |
| | | // ftpRemote.mkdir(timeStr); |
| | | // ftpRemote.disconnect(); |
| | | // //备份过程是否出现过问题,标识符 |
| | | // boolean isError=false; |
| | | // try { |
| | | // |
| | | // File docFile = new File(rootDoc); |
| | | // File[] fileList = docFile.listFiles(); |
| | | // //File[] fileList2 = new File[fileList.length]; |
| | | // //BeanUtils.copyProperties(fileList2,fileList); |
| | | // for (File file:fileList) { |
| | | // FtpHelper ftpTemp = new FtpHelper(YamlProperties.ftpIp, YamlProperties.ftpPort, YamlProperties.ftpUserName, YamlProperties.ftpPassword); |
| | | // //压缩传输 |
| | | // File fileZip = new File(file.getAbsolutePath() + ".zip"); |
| | | //ZipUtils.toZip(file.getAbsolutePath(),new FileOutputStream(fileZip),true); |
| | | String pathName=timeStr+"/"+file.getName()+".zip"; |
| | | //System.out.println("传输目标路径确认为:"+pathName); |
| | | ftpRemote.uploadFile2(fileZip, pathName); |
| | | //System.out.println(fileZip+"传输完毕"); |
| | | fileZip.delete(); |
| | | //System.out.println(fileZip+"传输中转压缩包删除完毕"); |
| | | } catch (Exception e) { |
| | | isError = true; |
| | | e.printStackTrace(); |
| | | fileZip.delete(); |
| | | } |
| | | } |
| | | //System.out.println("传输完毕"); |
| | | |
| | | //获取备份目录,保留4次. |
| | | //如果过程出现错误,则不删除 |
| | | if (!isError) { |
| | | String[] dirList = ftpRemote.getDirList(); |
| | | List<String> dirList2 = Arrays.asList(dirList); |
| | | |
| | | for (int i = 0; i < dirList2.size()-4; i++) { |
| | | ftpRemote.deleteDir(dirList2.get(i)); |
| | | } |
| | | } |
| | | } catch (Exception e) { |
| | | |
| | | }finally { |
| | | ftpRemote.disconnect(); |
| | | } |
| | | } |
| | | } |
| | | // //System.out.println(fileZip.toString()+"压缩完毕"); |
| | | // //System.out.println("进行FTP传输中..."); |
| | | // try { |
| | | // //ZipUtils.toZip(file.getAbsolutePath(),new FileOutputStream(fileZip),true); |
| | | // String pathName=timeStr+"/"+file.getName()+".zip"; |
| | | // //System.out.println("传输目标路径确认为:"+pathName); |
| | | // ftpTemp.uploadFile2(fileZip, pathName); |
| | | // //System.out.println(fileZip+"传输完毕"); |
| | | // fileZip.delete(); |
| | | // //System.out.println(fileZip+"传输中转压缩包删除完毕"); |
| | | // } catch (Exception e) { |
| | | // isError = true; |
| | | // e.printStackTrace(); |
| | | // fileZip.delete(); |
| | | // }finally { |
| | | // ftpTemp.disconnect(); |
| | | // } |
| | | // } |
| | | // //System.out.println("传输完毕"); |
| | | // |
| | | // //获取备份目录,保留4次. |
| | | // //如果过程出现错误,则不删除 |
| | | // if (!isError) { |
| | | // FtpHelper ftpTemp = new FtpHelper(YamlProperties.ftpIp, YamlProperties.ftpPort, YamlProperties.ftpUserName, YamlProperties.ftpPassword); |
| | | // String[] dirList = ftpRemote.getDirList(); |
| | | // List<String> dirList2 = Arrays.asList(dirList); |
| | | // |
| | | // for (int i = 0; i < dirList2.size()-4; i++) { |
| | | // ftpTemp.deleteDir(dirList2.get(i)); |
| | | // } |
| | | // ftpTemp.disconnect(); |
| | | // } |
| | | // } catch (Exception e) { |
| | | // e.printStackTrace(); |
| | | // } |
| | | // } |
| | | //} |