whycxzp
2023-09-25 318105e308c277420dd8f080afa35102ddddc7db
更新
3个文件已修改
84 ■■■■■ 已修改文件
src/main/java/com/whyc/dto/FtpHelper.java 13 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/service/FtpService.java 69 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/service/ProductService.java 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/dto/FtpHelper.java
@@ -160,13 +160,20 @@
            for (String s : typeArray) {
                s.trim();
                if (!checkDirectory(ftp, s)) {
                    ftp.mkdir(s);
                    if(!ftp.existsDirectory(s)) {
                        ftp.mkdir(s);
                    }
                    changeDirectory(s);
                }
            }
        }
        ftp.put(inStream, fileName);
        changeDirectory("/");
    }
    public void uploadFile2(File inFile,String fileName) throws IOException, FTPException {
        InputStream inStream = new FileInputStream(inFile);
        ftp.put(inStream, fileName);
    }
    /**
@@ -287,4 +294,8 @@
    public String[] getDirList() throws IOException, FTPException {
        return ftp.dir();
    }
    public void mkdir(String dirPath) throws IOException, FTPException {
        ftp.mkdir(dirPath);
    }
}
src/main/java/com/whyc/service/FtpService.java
@@ -25,7 +25,7 @@
@Service
@EnableScheduling   // 2.开启定时任务
public class FtpService {
    //定时上传指定目录下文件
    //定时上传指定目录下文件,每周五凌晨开启备份
    @Scheduled(cron = "59 59 23 ? * FRI")
    //@Scheduled(cron = "0/10 * * * * ?")
    private void sendFtpFile() throws IOException, FTPException, InterruptedException, ParseException {
@@ -37,44 +37,61 @@
        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();
        }
src/main/java/com/whyc/service/ProductService.java
@@ -985,7 +985,7 @@
        lockLog.setLockFlag(-1);
        productLockLogService.insert(lockLog);
        return new Response().setII(1,"新增完成");
        return new Response().set(1,true,"新增完成");
    }
    private void deleteAndInsert(Product product) {