Java的ftp工具类批量上传和下载文件

时间:2025-02-16 12:00:56
如果是SFTP的看这里, 基于java的批量上传下载的SFTP工具类

引入依赖(这里的commons-net不要使用1.6以前的,会有假死的bug)

<dependency>
            <groupId>commons-net</groupId>
            <artifactId>commons-net</artifactId>
            <version>3.6</version>
</dependency>

完整代码

package ;

import ;
import ;
import ;
import ;

import .*;

public class FtpUtil {

    /**
     * Description: 向FTP服务器上传文件
     *
     * @param host     FTP服务器hostname
     * @param port     FTP服务器端口
     * @param username FTP登录账号
     * @param password FTP登录密码
     * @param basePath FTP服务器基础目录
     * @param filePath FTP服务器文件存放路径。文件的路径为basePath+filePath
     * @param filename 上传到FTP服务器上的文件名
     * @param input    输入流
     * @return 成功返回true,否则返回false
     */
    public static boolean uploadFile(String host, int port, String username, String password, String basePath,
                                     String filePath, String filename, InputStream input) {
        boolean result = false;
        FTPClient ftp = new FTPClient();
        try {
            int reply;
            (host, port);// 连接FTP服务器
            // 如果采用默认端口,可以使用(host)的方式直接连接FTP服务器
            (username, password);// 登录
            reply = ();
            if (!(reply)) {
                ();
                (333);
                return result;
            }
            //切换到上传目录
            if (!(basePath + filePath)) {
                //如果目录不存在创建目录
                String[] dirs = ("/");
                String tempPath = basePath;
                for (String dir : dirs) {
                    if (null == dir || "".equals(dir)) continue;
                    tempPath += "/" + dir;
                    if (!(tempPath)) {  //进不去目录,说明该目录不存在
                        if (!(tempPath)) { //创建目录
                            //如果创建文件目录失败,则返回
                            ("创建文件目录" + tempPath + "失败");
                            return result;
                        } else {
                            //目录存在,则直接进入该目录
                            (tempPath);
                        }
                    }
                }
            }
            //设置上传文件的类型为二进制类型
            (FTP.BINARY_FILE_TYPE);
            //上传文件
            if (!(filename, input)) {
                return result;
            }
            ();
            ();
            result = true;
        } catch (IOException e) {
            ();
        } finally {
            if (()) {
                try {
                    ();
                } catch (IOException ioe) {
                }
            }
        }
        return result;
    }

    /**
     * Description: 从FTP服务器下载文件
     *
     * @param host       FTP服务器hostname
     * @param port       FTP服务器端口
     * @param username   FTP登录账号
     * @param password   FTP登录密码
     * @param remotePath FTP服务器上的相对路径
     * @param fileName   要下载的文件名
     * @param localPath  下载后保存到本地的路径
     * @return
     */
    public static boolean downloadFile(String host, int port, String username, String password, String remotePath,
                                       String fileName, String localPath) {
        boolean result = false;
        FTPClient ftp = new FTPClient();
        try {
            int reply;
            (host, port);
            // 如果采用默认端口,可以使用(host)的方式直接连接FTP服务器
            (username, password);// 登录
            reply = ();
            if (!(reply)) {
                ();
                return result;
            }
            (remotePath);// 转移到FTP服务器目录
            ();
            FTPFile[] fs = ();
            ();
            File filePath = new File(localPath);
            if (!()){
                ();
            }
            OutputStream is=null;
            for (FTPFile ff : fs) {
                if ("all".equalsIgnoreCase(fileName)){
                    File localFile = new File(localPath + "/" + ());
                    is = new FileOutputStream(localFile);
                    boolean b = ((), is);
                    ();

                }else if (().equals(fileName)) {
                    File localFile = new File(localPath + "/" + ());
                    is = new FileOutputStream(localFile);
                    ((), is);
                    ();
                }
            }
            if (is!=null){
                ();
            }
            ();
            result = true;
        } catch (IOException e) {
            ();
        } finally {
            if (()) {
                try {
                    ();
                } catch (IOException ioe) {
                }
            }
        }
        return result;
    }

    //ftp上传文件测试main函数
    public static void main(String[] args) {

        //这里的files是本地文件夹
        File file = new File("C:\\Users\\86158\\Desktop\\doc");
        File[] files = ();
        //批量上传
        try {
            for (int i = 0; i < ; i++) {
                File fe = files[i];
                FileInputStream in = new FileInputStream(fe);
                boolean flag = uploadFile("", 21, "xx", "xxx",
                        "/opt/wacos/server", "/sa", (), in);
                (flag);
            }

        } catch (FileNotFoundException e) {
            ();
        }

        //下载,如果fileName未指明文件名而是all的话,将会下载远程remotePath下的所有文件
        boolean b = downloadFile("", 21, "xx", "xxx",
                "/opt/wacos/server/smsFilters/dump/cmstat", "all", "C:\\Users\\86158\\Desktop\\doc");
        ("下载结果:"+b);
    }
}