| | |
| | | @Service |
| | | @EnableScheduling // 2.开启定时任务 |
| | | public class FtpService { |
| | | //定时上传指定目录下文件 |
| | | //定时上传指定目录下文件,每周五凌晨开启备份 |
| | | @Scheduled(cron = "59 59 23 ? * FRI") |
| | | //@Scheduled(cron = "0/10 * * * * ?") |
| | | private void sendFtpFile() throws IOException, FTPException, InterruptedException, ParseException { |
| | |
| | | for (int i = 0; i < dirList2.size()-3; i++) { |
| | | ftpRemote.deleteDir(dirList2.get(i)); |
| | | } |
| | | ftpRemote.disconnect(); |
| | | |
| | | |
| | | |
| | | String fileDirName = FileDirPath.getFileDirName(); |
| | | |
| | | String rootDoc=fileDirName+File.separator+"doc_file"; |
| | | String timeStr= ActionUtil.sdfwithFTP.format(new Date()); |
| | | ftpRemote.mkdir(timeStr); |
| | | ftpRemote.disconnect(); |
| | | try { |
| | | |
| | | File docFile = new File(rootDoc); |
| | | 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(); |
| | | 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(); |
| | | } |