From 98d5bcff9769b6757c155ba73541d1930fbc4f8a Mon Sep 17 00:00:00 2001
From: whycxzp <glperry@163.com>
Date: 星期四, 28 三月 2024 11:25:34 +0800
Subject: [PATCH] 经过最近多次调试,实际测试. 改用common.net插件进行远程备份,消除大文件备份中断问题

---
 src/main/java/com/whyc/dto/FtpHelper.java |  602 +++++++++++++++++++++++++++---------------------------
 1 files changed, 301 insertions(+), 301 deletions(-)

diff --git a/src/main/java/com/whyc/dto/FtpHelper.java b/src/main/java/com/whyc/dto/FtpHelper.java
index bf7db3c..a2274ac 100644
--- a/src/main/java/com/whyc/dto/FtpHelper.java
+++ b/src/main/java/com/whyc/dto/FtpHelper.java
@@ -1,301 +1,301 @@
-package com.whyc.dto;
-
-import com.enterprisedt.net.ftp.*;
-import org.apache.commons.lang.StringUtils;
-
-import java.io.*;
-import java.text.ParseException;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.LinkedList;
-import java.util.List;
-
-public class FtpHelper {
-    private FTPClient ftp;
-
-    /**
-     * 鍒濆鍖朏tp淇℃伅
-     *
-     * @param ftpServer   ftp鏈嶅姟鍣ㄥ湴鍧�
-     * @param ftpPort     Ftp绔彛鍙�
-     * @param ftpUsername ftp 鐢ㄦ埛鍚�
-     * @param ftpPassword ftp 瀵嗙爜
-     */
-    public FtpHelper(String ftpServer, int ftpPort, String ftpUsername,
-                     String ftpPassword) {
-        connect(ftpServer, ftpPort, ftpUsername, ftpPassword);
-    }
-
-    /**
-     * 杩炴帴鍒癴tp
-     *
-     * @param ftpServer   ftp鏈嶅姟鍣ㄥ湴鍧�
-     * @param ftpPort     Ftp绔彛鍙�
-     * @param ftpUsername ftp 鐢ㄦ埛鍚�
-     * @param ftpPassword ftp 瀵嗙爜
-     */
-    public void connect(String ftpServer, int ftpPort, String ftpUsername, String ftpPassword) {
-        ftp = new FTPClient();
-        try {
-            ftp.setControlEncoding("GBK");
-            ftp.setRemoteHost(ftpServer);
-            ftp.setRemotePort(ftpPort);
-            ftp.setTimeout(1000*60*60*12);
-            ftp.setConnectMode(FTPConnectMode.ACTIVE);
-            ftp.connect();
-            ftp.login(ftpUsername, ftpPassword);
-            ftp.setType(FTPTransferType.BINARY);
-        } catch (Exception e) {
-            e.printStackTrace();
-            ftp = null;
-        }
-    }
-
-    /**
-     * 鏇存敼ftp璺緞
-     *
-     * @param ftp
-     * @param dirName
-     * @return
-     */
-    public boolean checkDirectory(FTPClient ftp, String dirName) {
-        boolean flag;
-        try {
-            ftp.chdir(dirName);
-            flag = true;
-        } catch (Exception e) {
-            //e.printStackTrace();
-            flag = false;
-        }
-        return flag;
-    }
-
-    /**
-     * 鏂紑ftp閾炬帴
-     */
-    public void disconnect() {
-        try {
-            if (ftp.connected()) {
-                ftp.quit();
-            }
-        } catch (Exception e) {
-            e.printStackTrace();
-        }
-    }
-
-    /**
-     * 璇诲彇ftp鏂囦欢娴�
-     *
-     * @param filePath ftp鏂囦欢璺緞
-     * @return s
-     * @throws Exception
-     */
-    public InputStream downloadFile(String filePath) throws Exception {
-        InputStream inputStream = null;
-        String fileName = "";
-        filePath = StringUtils.removeStart(filePath, "/");
-        int len = filePath.lastIndexOf("/");
-        if (len == -1) {
-            if (filePath.length() > 0) {
-                fileName = filePath;
-            } else {
-                throw new Exception("娌℃湁杈撳叆鏂囦欢璺緞");
-            }
-        } else {
-            fileName = filePath.substring(len + 1);
-
-            String type = filePath.substring(0, len);
-            String[] typeArray = type.split("/");
-            for (String s : typeArray) {
-                ftp.chdir(s);
-            }
-        }
-        byte[] data;
-        try {
-            data = ftp.get(fileName);
-            inputStream = new ByteArrayInputStream(data);
-        } catch (Exception e) {
-            e.printStackTrace();
-        }
-        return inputStream;
-    }
-
-    /**
-     * 涓婁紶鏂囦欢鍒癴tp
-     *
-     * @param file     鏂囦欢瀵硅薄
-     * @param filePath 涓婁紶鐨勮矾寰�
-     * @throws Exception
-     */
-    public void uploadFile(File file, String filePath) throws Exception {
-        InputStream inStream = new FileInputStream(file);
-        uploadFile(inStream, filePath);
-    }
-
-    /**
-     * 涓婁紶鏂囦欢鍒癴tp
-     *
-     * @param inStream 涓婁紶鐨勬枃浠舵祦
-     * @param filePath 涓婁紶璺緞
-     * @throws Exception
-     */
-    public void uploadFile(InputStream inStream, String filePath)
-            throws Exception {
-        if (inStream == null) {
-            return;
-        }
-        String fileName = "";
-        filePath = StringUtils.removeStart(filePath, "/");
-        int len = filePath.lastIndexOf("/");
-        if (len == -1) {
-            if (filePath.length() > 0) {
-                fileName = filePath;
-            } else {
-                throw new Exception("娌℃湁杈撳叆鏂囦欢璺緞");
-            }
-        } else {
-            fileName = filePath.substring(len + 1);
-            String type = filePath.substring(0, len);
-            String[] typeArray = type.split("/");
-            for (String s : typeArray) {
-                s.trim();
-                if (!checkDirectory(ftp, 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);
-    }
-
-    /**
-     * 鍒犻櫎ftp鏂囦欢
-     *
-     * @param filePath 鏂囦欢璺緞
-     * @throws Exception
-     */
-    public void deleteFile(String filePath) throws Exception {
-        String fileName = "";
-        filePath = StringUtils.removeStart(filePath, "/");
-        int len = filePath.lastIndexOf("/");
-        if (len == -1) {
-            if (filePath.length() > 0) {
-                fileName = filePath;
-            } else {
-                throw new Exception("娌℃湁杈撳叆鏂囦欢璺緞");
-            }
-        } else {
-            fileName = filePath.substring(len + 1);
-
-            String type = filePath.substring(0, len);
-            String[] typeArray = type.split("/");
-            for (String s : typeArray) {
-                if (checkDirectory(ftp, s)) {
-                    ftp.chdir(s);
-                }
-            }
-        }
-        ftp.delete(fileName);
-    }
-
-
-     //鍒囨崲鐩綍
-    public void changeDirectory(String path) {
-        if (!StringUtils.isEmpty(path)) {
-            try {
-                ftp.chdir(path);
-            } catch (Exception e) {
-                e.printStackTrace();
-            }
-        }
-    }
-
-    //璇诲彇鏂囦欢鍒楄〃
-    public List<File> getFileList(String path) {
-        List<File> rsList = new ArrayList<>();
-        if (path == null || path.equals("")) {
-            return rsList;
-        }
-        File file = new File(path);
-        if (!file.exists()) {
-            return rsList;
-        }
-        // 澶勭悊鏂囦欢
-        if (file.isFile()) {
-            rsList.add(file);
-            return rsList;
-        }
-        // 澶勭悊鏂囦欢澶�
-        if (null == file.listFiles()) {
-            rsList.add(file);
-            return rsList;
-        }
-        LinkedList<File> forDealList = new LinkedList<>();
-        forDealList.addAll(Arrays.asList(file.listFiles()));
-        while (!forDealList.isEmpty()) {
-            File firstFile = forDealList.removeFirst();
-            // 澶勭悊鏂囦欢
-            if (firstFile.isFile()) {
-                // 鎶婃枃浠惰嚜韬厛鍔犲叆缁撴灉
-                rsList.add(firstFile);
-                continue;
-            }
-            // 澶勭悊鏂囦欢澶�
-            File[] files = firstFile.listFiles();
-            if (files == null) {
-                continue;
-            }
-            for (File curr : files) {
-                if (curr.isDirectory()) {
-                    // 灏嗘鏂囦欢澶规斁鍏ュ緟澶勭悊鍒楄〃
-                    forDealList.add(curr);
-                } else {
-                    rsList.add(curr);
-                }
-            }
-        }
-        return rsList;
-    }
-
-    public void deleteDir(String dirName) throws IOException, FTPException, ParseException {
-        FTPClient client = ftp;
-
-        //String[] dir = client.dir(dirName, true);
-        FTPFile[] ftpFiles = client.dirDetails(dirName);
-        for (FTPFile ftpFile : ftpFiles) {
-            if(ftpFile.isDir()){
-                deleteDir(dirName+"/"+ftpFile.getName());
-            }else{
-                client.delete(dirName+"/"+ftpFile.getName());
-            }
-        }
-        /*for (String subDirName : dir) {
-            String name = subDirName.substring(subDirName.lastIndexOf(" ") + 1);
-            if(subDirName.contains("<DIR>")){ //鏂囦欢澶�
-                //name = new String(name.getBytes(StandardCharsets.ISO_8859_1),"GBK");
-                deleteDir(dirName+"/"+name);
-                //client.rmdir(dirName+"/"+name);
-            }else{
-                //name = new String(name.getBytes(StandardCharsets.ISO_8859_1),"GBK");
-                client.delete(dirName+"/"+name);
-            }
-        }*/
-        client.rmdir(dirName);
-    }
-
-    public String[] getDirList() throws IOException, FTPException {
-        return ftp.dir();
-    }
-
-    public void mkdir(String dirPath) throws IOException, FTPException {
-        ftp.mkdir(dirPath);
-    }
-}
+//package com.whyc.dto;
+//
+//import com.enterprisedt.net.ftp.*;
+//import org.apache.commons.lang.StringUtils;
+//
+//import java.io.*;
+//import java.text.ParseException;
+//import java.util.ArrayList;
+//import java.util.Arrays;
+//import java.util.LinkedList;
+//import java.util.List;
+//
+//public class FtpHelper {
+//    private FTPClient ftp;
+//
+//    /**
+//     * 鍒濆鍖朏tp淇℃伅
+//     *
+//     * @param ftpServer   ftp鏈嶅姟鍣ㄥ湴鍧�
+//     * @param ftpPort     Ftp绔彛鍙�
+//     * @param ftpUsername ftp 鐢ㄦ埛鍚�
+//     * @param ftpPassword ftp 瀵嗙爜
+//     */
+//    public FtpHelper(String ftpServer, int ftpPort, String ftpUsername,
+//                     String ftpPassword) {
+//        connect(ftpServer, ftpPort, ftpUsername, ftpPassword);
+//    }
+//
+//    /**
+//     * 杩炴帴鍒癴tp
+//     *
+//     * @param ftpServer   ftp鏈嶅姟鍣ㄥ湴鍧�
+//     * @param ftpPort     Ftp绔彛鍙�
+//     * @param ftpUsername ftp 鐢ㄦ埛鍚�
+//     * @param ftpPassword ftp 瀵嗙爜
+//     */
+//    public void connect(String ftpServer, int ftpPort, String ftpUsername, String ftpPassword) {
+//        ftp = new FTPClient();
+//        try {
+//            ftp.setControlEncoding("GBK");
+//            ftp.setRemoteHost(ftpServer);
+//            ftp.setRemotePort(ftpPort);
+//            ftp.setTimeout(1000*60*60*12);
+//            ftp.setConnectMode(FTPConnectMode.ACTIVE);
+//            ftp.connect();
+//            ftp.login(ftpUsername, ftpPassword);
+//            ftp.setType(FTPTransferType.BINARY);
+//        } catch (Exception e) {
+//            e.printStackTrace();
+//            ftp = null;
+//        }
+//    }
+//
+//    /**
+//     * 鏇存敼ftp璺緞
+//     *
+//     * @param ftp
+//     * @param dirName
+//     * @return
+//     */
+//    public boolean checkDirectory(FTPClient ftp, String dirName) {
+//        boolean flag;
+//        try {
+//            ftp.chdir(dirName);
+//            flag = true;
+//        } catch (Exception e) {
+//            //e.printStackTrace();
+//            flag = false;
+//        }
+//        return flag;
+//    }
+//
+//    /**
+//     * 鏂紑ftp閾炬帴
+//     */
+//    public void disconnect() {
+//        try {
+//            if (ftp.connected()) {
+//                ftp.quit();
+//            }
+//        } catch (Exception e) {
+//            e.printStackTrace();
+//        }
+//    }
+//
+//    /**
+//     * 璇诲彇ftp鏂囦欢娴�
+//     *
+//     * @param filePath ftp鏂囦欢璺緞
+//     * @return s
+//     * @throws Exception
+//     */
+//    public InputStream downloadFile(String filePath) throws Exception {
+//        InputStream inputStream = null;
+//        String fileName = "";
+//        filePath = StringUtils.removeStart(filePath, "/");
+//        int len = filePath.lastIndexOf("/");
+//        if (len == -1) {
+//            if (filePath.length() > 0) {
+//                fileName = filePath;
+//            } else {
+//                throw new Exception("娌℃湁杈撳叆鏂囦欢璺緞");
+//            }
+//        } else {
+//            fileName = filePath.substring(len + 1);
+//
+//            String type = filePath.substring(0, len);
+//            String[] typeArray = type.split("/");
+//            for (String s : typeArray) {
+//                ftp.chdir(s);
+//            }
+//        }
+//        byte[] data;
+//        try {
+//            data = ftp.get(fileName);
+//            inputStream = new ByteArrayInputStream(data);
+//        } catch (Exception e) {
+//            e.printStackTrace();
+//        }
+//        return inputStream;
+//    }
+//
+//    /**
+//     * 涓婁紶鏂囦欢鍒癴tp
+//     *
+//     * @param file     鏂囦欢瀵硅薄
+//     * @param filePath 涓婁紶鐨勮矾寰�
+//     * @throws Exception
+//     */
+//    public void uploadFile(File file, String filePath) throws Exception {
+//        InputStream inStream = new FileInputStream(file);
+//        uploadFile(inStream, filePath);
+//    }
+//
+//    /**
+//     * 涓婁紶鏂囦欢鍒癴tp
+//     *
+//     * @param inStream 涓婁紶鐨勬枃浠舵祦
+//     * @param filePath 涓婁紶璺緞
+//     * @throws Exception
+//     */
+//    public void uploadFile(InputStream inStream, String filePath)
+//            throws Exception {
+//        if (inStream == null) {
+//            return;
+//        }
+//        String fileName = "";
+//        filePath = StringUtils.removeStart(filePath, "/");
+//        int len = filePath.lastIndexOf("/");
+//        if (len == -1) {
+//            if (filePath.length() > 0) {
+//                fileName = filePath;
+//            } else {
+//                throw new Exception("娌℃湁杈撳叆鏂囦欢璺緞");
+//            }
+//        } else {
+//            fileName = filePath.substring(len + 1);
+//            String type = filePath.substring(0, len);
+//            String[] typeArray = type.split("/");
+//            for (String s : typeArray) {
+//                s.trim();
+//                if (!checkDirectory(ftp, 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);
+//    }
+//
+//    /**
+//     * 鍒犻櫎ftp鏂囦欢
+//     *
+//     * @param filePath 鏂囦欢璺緞
+//     * @throws Exception
+//     */
+//    public void deleteFile(String filePath) throws Exception {
+//        String fileName = "";
+//        filePath = StringUtils.removeStart(filePath, "/");
+//        int len = filePath.lastIndexOf("/");
+//        if (len == -1) {
+//            if (filePath.length() > 0) {
+//                fileName = filePath;
+//            } else {
+//                throw new Exception("娌℃湁杈撳叆鏂囦欢璺緞");
+//            }
+//        } else {
+//            fileName = filePath.substring(len + 1);
+//
+//            String type = filePath.substring(0, len);
+//            String[] typeArray = type.split("/");
+//            for (String s : typeArray) {
+//                if (checkDirectory(ftp, s)) {
+//                    ftp.chdir(s);
+//                }
+//            }
+//        }
+//        ftp.delete(fileName);
+//    }
+//
+//
+//     //鍒囨崲鐩綍
+//    public void changeDirectory(String path) {
+//        if (!StringUtils.isEmpty(path)) {
+//            try {
+//                ftp.chdir(path);
+//            } catch (Exception e) {
+//                e.printStackTrace();
+//            }
+//        }
+//    }
+//
+//    //璇诲彇鏂囦欢鍒楄〃
+//    public List<File> getFileList(String path) {
+//        List<File> rsList = new ArrayList<>();
+//        if (path == null || path.equals("")) {
+//            return rsList;
+//        }
+//        File file = new File(path);
+//        if (!file.exists()) {
+//            return rsList;
+//        }
+//        // 澶勭悊鏂囦欢
+//        if (file.isFile()) {
+//            rsList.add(file);
+//            return rsList;
+//        }
+//        // 澶勭悊鏂囦欢澶�
+//        if (null == file.listFiles()) {
+//            rsList.add(file);
+//            return rsList;
+//        }
+//        LinkedList<File> forDealList = new LinkedList<>();
+//        forDealList.addAll(Arrays.asList(file.listFiles()));
+//        while (!forDealList.isEmpty()) {
+//            File firstFile = forDealList.removeFirst();
+//            // 澶勭悊鏂囦欢
+//            if (firstFile.isFile()) {
+//                // 鎶婃枃浠惰嚜韬厛鍔犲叆缁撴灉
+//                rsList.add(firstFile);
+//                continue;
+//            }
+//            // 澶勭悊鏂囦欢澶�
+//            File[] files = firstFile.listFiles();
+//            if (files == null) {
+//                continue;
+//            }
+//            for (File curr : files) {
+//                if (curr.isDirectory()) {
+//                    // 灏嗘鏂囦欢澶规斁鍏ュ緟澶勭悊鍒楄〃
+//                    forDealList.add(curr);
+//                } else {
+//                    rsList.add(curr);
+//                }
+//            }
+//        }
+//        return rsList;
+//    }
+//
+//    public void deleteDir(String dirName) throws IOException, FTPException, ParseException {
+//        FTPClient client = ftp;
+//
+//        //String[] dir = client.dir(dirName, true);
+//        FTPFile[] ftpFiles = client.dirDetails(dirName);
+//        for (FTPFile ftpFile : ftpFiles) {
+//            if(ftpFile.isDir()){
+//                deleteDir(dirName+"/"+ftpFile.getName());
+//            }else{
+//                client.delete(dirName+"/"+ftpFile.getName());
+//            }
+//        }
+//        /*for (String subDirName : dir) {
+//            String name = subDirName.substring(subDirName.lastIndexOf(" ") + 1);
+//            if(subDirName.contains("<DIR>")){ //鏂囦欢澶�
+//                //name = new String(name.getBytes(StandardCharsets.ISO_8859_1),"GBK");
+//                deleteDir(dirName+"/"+name);
+//                //client.rmdir(dirName+"/"+name);
+//            }else{
+//                //name = new String(name.getBytes(StandardCharsets.ISO_8859_1),"GBK");
+//                client.delete(dirName+"/"+name);
+//            }
+//        }*/
+//        client.rmdir(dirName);
+//    }
+//
+//    public String[] getDirList() throws IOException, FTPException {
+//        return ftp.dir();
+//    }
+//
+//    public void mkdir(String dirPath) throws IOException, FTPException {
+//        ftp.mkdir(dirPath);
+//    }
+//}

--
Gitblit v1.9.1