Commons Net 实现 FTP上传下载等常用操作

本文引自大佬文章,微调,仅作笔录使用,如有冒犯,留言即删!

直接上代码(示例下载)

1.Maven依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.2.1.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.lee.demo</groupId>
	<artifactId>ftp</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>ftp</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.12</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.17</version>
		</dependency>

		<dependency>
			<groupId>commons-net</groupId>
			<artifactId>commons-net</artifactId>
			<version>3.6</version>
		</dependency>
		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-lang3</artifactId>
			<version>3.9</version>
		</dependency>
		<dependency>
			<groupId>commons-io</groupId>
			<artifactId>commons-io</artifactId>
			<version>2.4</version>
		</dependency>
		<dependency>
			<groupId>javax.ws.rs</groupId>
			<artifactId>jsr311-api</artifactId>
			<version>1.1.1</version>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
			<exclusions>
				<exclusion>
					<groupId>org.junit.vintage</groupId>
					<artifactId>junit-vintage-engine</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

2.FTP工具类

package com.lee.demo.ftp.util;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.TrueFileFilter;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.log4j.Logger;

import java.io.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;


/**
 * 创建人:Jimmy
 * 创建时间:2019年11月11日23:24
 * 描述: FTP工具类
 */
public class FtpUtil {

    private static Logger logger = Logger.getLogger(FtpUtil.class.getName());

    /**
     * 连接 FTP 服务器
     *
     * @param addr     FTP 服务器 IP 地址
     * @param port     FTP 服务器端口号
     * @param username 登录用户名
     * @param password 登录密码
     * @return
     * @throws Exception
     */
    public static FTPClient connectFtpServer(String addr, int port, String username, String password, String controlEncoding) {
        FTPClient ftpClient = new FTPClient();
        try {
            /**设置文件传输的编码*/
            ftpClient.setControlEncoding(controlEncoding);

            /**连接 FTP 服务器
             * 如果连接失败,则此时抛出异常,如ftp服务器服务关闭时,抛出异常:
             * java.net.ConnectException: Connection refused: connect*/
            ftpClient.connect(addr, port);
            /**登录 FTP 服务器
             * 1)如果传入的账号为空,则使用匿名登录,此时账号使用 "Anonymous",密码为空即可*/
            if (StringUtils.isBlank(username)) {
                ftpClient.login("", "");
            } else {
                ftpClient.login(username, password);
            }

            /** 设置传输的文件类型
             * BINARY_FILE_TYPE:二进制文件类型
             * ASCII_FILE_TYPE:ASCII传输方式,这是默认的方式
             * ....
             */
            ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);

            /**
             * 确认应答状态码是否正确完成响应
             * 凡是 2开头的 isPositiveCompletion 都会返回 true,因为它底层判断是:
             * return (reply >= 200 && reply < 300);
             */
            int reply = ftpClient.getReplyCode();
            ftpClient.enterLocalPassiveMode();
            if (!FTPReply.isPositiveCompletion(reply)) {
                /**
                 * 如果 FTP 服务器响应错误 中断传输、断开连接
                 * abort:中断文件正在进行的文件传输,成功时返回 true,否则返回 false
                 * disconnect:断开与服务器的连接,并恢复默认参数值
                 */
                ftpClient.abort();
                ftpClient.disconnect();
            } else {
            }
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println(">>>>>FTP服务器连接登录失败,请检查连接参数是否正确,或者网络是否通畅*********");
        }
        return ftpClient;
    }

    /**
     * 下载 FTP 服务器上指定的单个文件,而且本地存放的文件相对部分路径 会与 FTP 服务器结构保持一致
     *
     * @param ftpClient              :连接成功有效的 FTP客户端连接
     * @param absoluteLocalDirectory :本地存储文件的绝对路径,如 E:\gxg\ftpDownload
     * @param relativeRemotePath     :ftpFile 文件在服务器所在的绝对路径,此方法强制路径使用右斜杠"\",如 "\video\2018.mp4"
     * @return
     */
    public static void downloadSingleFile(FTPClient ftpClient, String absoluteLocalDirectory, String relativeRemotePath) {
        /**如果 FTP 连接已经关闭,或者连接无效,则直接返回*/
        if (!ftpClient.isConnected() || !ftpClient.isAvailable()) {
            System.out.println(">>>>>FTP服务器连接已经关闭或者连接无效*********");
            return;
        }
        if (StringUtils.isBlank(absoluteLocalDirectory) || StringUtils.isBlank(relativeRemotePath)) {
            System.out.println(">>>>>下载时遇到本地存储路径或者ftp服务器文件路径为空,放弃...*********");
            return;
        }
        try {
            /**没有对应路径时,FTPFile[] 大小为0,不会为null*/
            FTPFile[] ftpFiles = ftpClient.listFiles(relativeRemotePath);
            FTPFile ftpFile = null;
            if (ftpFiles.length >= 1) {
                ftpFile = ftpFiles[0];
            }
            if (ftpFile != null && ftpFile.isFile()) {
                /** ftpFile.getName():获取的是文件名称,如 123.mp4
                 * 必须保证文件存放的父目录必须存在,否则 retrieveFile 保存文件时报错
                 */
                File localFile = new File(absoluteLocalDirectory, relativeRemotePath);
                if (!localFile.getParentFile().exists()) {
                    localFile.getParentFile().mkdirs();
                }

                OutputStream outputStream = new FileOutputStream(localFile);

                String workDir = relativeRemotePath.substring(0, relativeRemotePath.lastIndexOf("/"));
                if (StringUtils.isBlank(workDir)) {
                    workDir = "/";
                }
                /**文件下载前,FTPClient工作目录必须切换到文件所在的目录,否则下载失败
                 * "/" 表示用户根目录*/
                ftpClient.changeWorkingDirectory(workDir);
                /**下载指定的 FTP 文件 到本地
                 * 1)注意只能是文件,不能直接下载整个目录
                 * 2)如果文件本地已经存在,默认会重新下载
                 * 3)下载文件之前,ftpClient 工作目录必须是下载文件所在的目录
                 * 4)下载成功返回 true,失败返回 false
                 */
                ftpClient.retrieveFile(ftpFile.getName(), outputStream);

                outputStream.flush();
                outputStream.close();
                System.out.println(">>>>>FTP服务器文件下载完毕*********" + ftpFile.getName());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 遍历 FTP 服务器指定目录下的所有文件(包含子孙文件)
     *
     * @param ftpClient        :连接成功有效的 FTP客户端连接
     * @param remotePath       :查询的 FTP 服务器目录,如果文件,则视为无效,使用绝对路径,如"/"、"/video"、"/"、"/video"
     * @param relativePathList :返回查询结果,其中为服务器目录下的文件相对路径,如:\1.png、\docs\overview-tree.html 等
     * @return
     */
    public static List<String> loopServerPath(FTPClient ftpClient, String remotePath, List<String> relativePathList) {
        /**如果 FTP 连接已经关闭,或者连接无效,则直接返回*/
        if (!ftpClient.isConnected() || !ftpClient.isAvailable()) {
            System.out.println("ftp 连接已经关闭或者连接无效......");
            return relativePathList;
        }
        try {
            /**转移到FTP服务器根目录下的指定子目录
             * 1)"/":表示用户的根目录,为空时表示不变更
             * 2)参数必须是目录,当是文件时改变路径无效
             * */
            ftpClient.changeWorkingDirectory(remotePath);
            /** listFiles:获取FtpClient连接的当前下的一级文件列表(包括子目录)
             * 1)FTPFile[] ftpFiles = ftpClient.listFiles("/docs/info");
             *      获取服务器指定目录下的子文件列表(包括子目录),以 FTP 登录用户的根目录为基准,与 FTPClient 当前连接目录无关
             * 2)FTPFile[] ftpFiles = ftpClient.listFiles("/docs/info/springmvc.txt");
             *      获取服务器指定文件,此时如果文件存在时,则 FTPFile[] 大小为 1,就是此文件
             * */
            FTPFile[] ftpFiles = ftpClient.listFiles();
            if (ftpFiles != null && ftpFiles.length > 0) {
                for (FTPFile ftpFile : ftpFiles) {
                    if (ftpFile.isFile()) {
                        String relativeRemotePath = remotePath + "/" + ftpFile.getName();
                        relativePathList.add(relativeRemotePath);
                    } else {
                        loopServerPath(ftpClient, remotePath + "/" + ftpFile.getName(), relativePathList);
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return relativePathList;
    }

    /**
     * 同步本地目录与 FTP 服务器目录
     * 1)约定:FTP 服务器有,而本地没有的,则下载下来;本地有,而ftp服务器没有的,则将本地多余的删除
     * 2)始终确保本地与 ftp 服务器内容一致
     * 2)让 FTP 服务器与 本地目录保持结构一致,如 服务器上是 /docs/overview-tree.html,则本地也是 localDir/docs/overview-tree.html
     *
     * @param ftpClient        连接成功有效的 FTPClinet
     * @param localSyncFileDir :与 FTP 目录进行同步的本地目录
     */
    public static void syncLocalDir(FTPClient ftpClient, String localSyncFileDir) throws IOException {
        /**如果 FTP 连接已经关闭,或者连接无效,则直接返回*/
        if (!ftpClient.isConnected() || !ftpClient.isAvailable() || StringUtils.isBlank(localSyncFileDir)) {
            System.out.println(">>>>>FTP服务器连接已经关闭或者连接无效*********");
            return;
        }

        /** 获取本地存储目录下的文件*/
        Collection<File> fileCollection = localListFiles(new File(localSyncFileDir));
        System.out.println(">>>>>本地存储目录共有文件数量*********" + fileCollection.size());

        /**获取 FTP 服务器下的相对路径*/
        List<String> relativePathList = new ArrayList<>();
        relativePathList = loopServerPath(ftpClient, "", relativePathList);
        System.out.println(">>>>>FTP 服务器端共有文件数量*********" + relativePathList.size());

        /**
         * 遍历 本地存储目录下的文件
         * 1)如果本地文件在 FTP 服务器上不存在,则删除它
         * 2)如果本地文件在 FTP 服务器上存在,则比较两种大小
         *    如果大小不一致,则重新下载
         */
        for (File localFile : fileCollection) {
            String localFilePath = localFile.getPath();
            String localFileSuffi = localFilePath.replace(localSyncFileDir, "");
            if (relativePathList.contains(localFileSuffi)) {
                /**本地此文件在 FTP 服务器存在
                 * 1)比较大小,如果本地文件与服务器文件大小一致,则跳过
                 * 2)如果大小不一致,则删除本地文件,重新下载
                 * 3)最后都要删除relativePathList中的此元素,减轻后一次循环的压力*/
                FTPFile[] ftpFiles = ftpClient.listFiles(localFileSuffi);
                System.out.println(">>>>>本地文件 在 FTP 服务器已存在*********" + localFile.getPath());
                if (ftpFiles.length >= 1 && localFile.length() != ftpFiles[0].getSize()) {
                    downloadSingleFile(ftpClient, localSyncFileDir, localFileSuffi);
                    System.out.println(">>>>>本地文件与 FTP 服务器文件大小不一致,重新下载*********" + localFile.getPath());
                }
                relativePathList.remove(localFileSuffi);
            } else {
                System.out.println(">>>>>本地文件在 FTP 服务器不存在,删除本地文件*********" + localFile.getPath());
                /**本地此文件在 FTP 服务器不存在
                 * 1)删除本地文件
                 * 2)如果当前文件所在目录下文件已经为空,则将此父目录也一并删除*/
                localFile.delete();
                File parentFile = localFile.getParentFile();
                while (parentFile.list().length == 0) {
                    parentFile.delete();
                    parentFile = parentFile.getParentFile();
                }
            }
        }
        for (int i = 0; i < relativePathList.size(); i++) {
            System.out.println(">>>>> FTP 服务器存在新文件,准备下载*********" + relativePathList.get(i));
            downloadSingleFile(ftpClient, localSyncFileDir, relativePathList.get(i));
        }
    }

    /**
     * 遍历目录下的所有文件--方式1
     *
     * @param targetDir
     */
    public static Collection<File> localListFiles(File targetDir) {
        Collection<File> fileCollection = new ArrayList<>();
        if (targetDir != null && targetDir.exists() && targetDir.isDirectory()) {
            /**
             * targetDir:不要为 null、不要是文件、不要不存在
             * 第二个 文件过滤 参数如果为 FalseFileFilter.FALSE ,则不会查询任何文件
             * 第三个 目录过滤 参数如果为 FalseFileFilter.FALSE , 则只获取目标文件夹下的一级文件,而不会迭代获取子文件夹下的文件
             */
            fileCollection = FileUtils.listFiles(targetDir, TrueFileFilter.INSTANCE, TrueFileFilter.INSTANCE);
        }
        return fileCollection;
    }


    /**
     * 上传本地文件 或 目录 至 FTP 服务器----保持 FTP 服务器与本地 文件目录结构一致
     *
     * @param ftpClient  连接成功有效的 FTPClinet
     * @param uploadFile 待上传的文件 或 文件夹(此时会遍历逐个上传)
     * @throws Exception
     */
    public static void uploadFiles(FTPClient ftpClient, File uploadFile) {
        /**如果 FTP 连接已经关闭,或者连接无效,则直接返回*/
        if (!ftpClient.isConnected() || !ftpClient.isAvailable()) {
            System.out.println(">>>>>FTP服务器连接已经关闭或者连接无效*****放弃文件上传****");
            return;
        }
        if (uploadFile == null || !uploadFile.exists()) {
            System.out.println(">>>>>待上传文件为空或者文件不存在*****放弃文件上传****");
            return;
        }
        try {
            if (uploadFile.isDirectory()) {
                /**如果被上传的是目录时
                 * makeDirectory:在 FTP 上创建目录(方法执行完,服务器就会创建好目录,如果目录本身已经存在,则不会再创建)
                 * 1)可以是相对路径,即不以"/"开头,相对的是 FTPClient 当前的工作路径,如 "video"、"视频" 等,会在当前工作目录进行新建目录
                 * 2)可以是绝对路径,即以"/"开头,与 FTPCLient 当前工作目录无关,如 "/images"、"/images/2018"
                 * 3)注意多级目录时,必须确保父目录存在,否则创建失败,
                 *      如 "video/201808"、"/images/2018" ,如果 父目录 video与images不存在,则创建失败
                 * */
                ftpClient.makeDirectory(uploadFile.getName());
                /**变更 FTPClient 工作目录到新目录
                 * 1)不以"/"开头表示相对路径,新目录以当前工作目录为基准,即当前工作目录下不存在此新目录时,变更失败
                 * 2)参数必须是目录,当是文件时改变路径无效*/
                ftpClient.changeWorkingDirectory(uploadFile.getName());

                File[] listFiles = uploadFile.listFiles();
                for (File loopFile:listFiles) {
                    if (loopFile.isDirectory()) {
                        /**如果有子目录,则迭代调用方法进行上传*/
                        uploadFiles(ftpClient, loopFile);
                        /**changeToParentDirectory:将 FTPClient 工作目录移到上一层
                         * 这一步细节很关键,子目录上传完成后,必须将工作目录返回上一层,否则容易导致文件上传后,目录不一致
                         * */
                        ftpClient.changeToParentDirectory();
                    } else {
                        /**如果目录中全是文件,则直接上传*/
                        FileInputStream input = new FileInputStream(loopFile);
                        ftpClient.storeFile(loopFile.getName(), input);
                        input.close();
                        System.out.println(">>>>>文件上传成功****" + loopFile.getPath());
                    }
                }
            } else {
                /**如果被上传的是文件时*/
                FileInputStream input = new FileInputStream(uploadFile);
                /** storeFile:将本地文件上传到服务器
                 * 1)如果服务器已经存在此文件,则不会重新覆盖,即不会再重新上传
                 * 2)如果当前连接FTP服务器的用户没有写入的权限,则不会上传成功,但是也不会报错抛异常
                 * */
                ftpClient.storeFile(uploadFile.getName(), input);
                input.close();
                System.out.println(">>>>>文件上传成功****" + uploadFile.getPath());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /***
     * @上传文件夹
     * @param localDirectory 当地文件夹
     * @param remoteDirectoryPath Ftp 服务器路径 以目录"/"结束
     * */
    public static boolean uploadDirectory(FTPClient ftpClient,String localDirectory,String remoteDirectoryPath) {
        File src = new File(localDirectory);
        try {
            remoteDirectoryPath = remoteDirectoryPath + src.getName() + "/";
            ftpClient.makeDirectory(remoteDirectoryPath);
        } catch (IOException e) {
            e.printStackTrace();
            logger.info(remoteDirectoryPath + "目录创建失败");
        }
        File[] allFile = src.listFiles();
        for (File file:allFile) {
            if (file.isDirectory()) {
                // 递归
                uploadDirectory(ftpClient,file.getPath(),remoteDirectoryPath);
            }else {
                String srcName = file.getPath();
                uploadFile(ftpClient,new File(srcName),remoteDirectoryPath);
            }
        }
        return true;
    }


    /***
     * 上传Ftp文件
     * @param localFile 当地文件
     * @param romotUpLoadePath 上传服务器路径 - 应该以/结束
     * */
    public static boolean uploadFile(FTPClient ftpClient,File localFile,String romotUpLoadePath) {
        BufferedInputStream inStream = null;
        boolean success = false;
        try {
            ftpClient.changeWorkingDirectory(romotUpLoadePath);// 改变工作路径
            inStream = new BufferedInputStream(new FileInputStream(localFile));
            logger.info(localFile.getName() + "开始上传.....");
            success = ftpClient.storeFile(localFile.getName(), inStream);
            if (success == true) {
                logger.info(localFile.getName() + "上传成功");
                return success;
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            logger.error(localFile + "未找到");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (inStream != null) {
                try {
                    inStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return success;
    }

    /***
     * @下载文件夹
     * @param localDirectoryPath 本地地址
     * @param remoteDirectory 远程文件夹
     * */
    public static boolean downLoadDirectory(FTPClient ftpClient, String localDirectoryPath,String remoteDirectory) {
        try {
            String fileName = new File(remoteDirectory).getName();
            localDirectoryPath = localDirectoryPath + fileName + "/";
            new File(localDirectoryPath).mkdirs();
            FTPFile[] allFile = ftpClient.listFiles(remoteDirectory);
            for (FTPFile file:allFile) {
                if (file.isDirectory()) {
                    String strremoteDirectoryPath = remoteDirectory + "/" + file.getName();
                    downLoadDirectory(ftpClient, localDirectoryPath, strremoteDirectoryPath);
                }else {
                    downloadFile(ftpClient,file.getName(), localDirectoryPath, remoteDirectory);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
            logger.info("下载文件夹失败");
            return false;
        }
        return true;
    }

    /***
     * 下载文件
     * @param remoteFileName   待下载文件名称
     * @param localDires 下载到当地那个路径下
     * @param remoteDownLoadPath remoteFileName所在的路径
     */

    public static boolean downloadFile(FTPClient ftpClient, String remoteFileName, String localDires,
                                String remoteDownLoadPath) {
        String strFilePath = localDires + remoteFileName;
        BufferedOutputStream outStream = null;
        boolean success = false;
        try {
            ftpClient.changeWorkingDirectory(remoteDownLoadPath);
            outStream = new BufferedOutputStream(new FileOutputStream(strFilePath));
            logger.info(remoteFileName + "开始下载....");
            success = ftpClient.retrieveFile(remoteFileName, outStream);
            if (success == true) {
                logger.info(remoteFileName + "成功下载到" + strFilePath);
                return success;
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(remoteFileName + "下载失败");
        } finally {
            if (null != outStream) {
                try {
                    outStream.flush();
                    outStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        if (success == false) {
            logger.error(remoteFileName + "下载失败!!!");
        }
        return success;
    }


    /**
     * 删除服务器的文件
     *
     * @param ftpClient   连接成功且有效的 FTP客户端
     * @param deleteFiles 待删除的文件或者目录,为目录时,会逐个删除,
     *                    路径必须是绝对路径,如 "/1.png"、"/video/3.mp4"、"/images/2018"
     *                    "/" 表示用户根目录,则删除所有内容
     */
    public static void deleteServerFiles(FTPClient ftpClient, String deleteFiles) {
        /**如果 FTP 连接已经关闭,或者连接无效,则直接返回*/
        if (!ftpClient.isConnected() || !ftpClient.isAvailable()) {
            System.out.println(">>>>>FTP服务器连接已经关闭或者连接无效*****放弃文件上传****");
            return;
        }
        try {
            /** 尝试改变当前工作目录到 deleteFiles
             * 1)changeWorkingDirectory:变更FTPClient当前工作目录,变更成功返回true,否则失败返回false
             * 2)如果变更工作目录成功,则表示 deleteFiles 为服务器已经存在的目录
             * 3)否则变更失败,则认为 deleteFiles 是文件,是文件时则直接删除
             */
            boolean changeFlag = ftpClient.changeWorkingDirectory(deleteFiles);
            if (changeFlag) {
                /**当被删除的是目录时*/
                FTPFile[] ftpFiles = ftpClient.listFiles();
                for (FTPFile ftpFile : ftpFiles) {
                    System.out.println("----------------::::" + ftpClient.printWorkingDirectory());
                    if (ftpFile.isFile()) {
                        boolean deleteFlag = ftpClient.deleteFile(ftpFile.getName());
                        if (deleteFlag) {
                            System.out.println(">>>>>删除服务器文件成功****" + ftpFile.getName());
                        } else {
                            System.out.println(">>>>>删除服务器文件失败****" + ftpFile.getName());
                        }
                    } else {
                        /**printWorkingDirectory:获取 FTPClient 客户端当前工作目录
                         * 然后开始迭代删除子目录
                         */
                        String workingDirectory = ftpClient.printWorkingDirectory();
                        deleteServerFiles(ftpClient, workingDirectory + "/" + ftpFile.getName());
                    }
                }
                /**printWorkingDirectory:获取 FTPClient 客户端当前工作目录
                 * removeDirectory:删除FTP服务端的空目录,注意如果目录下存在子文件或者子目录,则删除失败
                 * 运行到这里表示目录下的内容已经删除完毕,此时再删除当前的为空的目录,同时将工作目录移动到上移层级
                 * */
                String workingDirectory = ftpClient.printWorkingDirectory();
                ftpClient.removeDirectory(workingDirectory);
                ftpClient.changeToParentDirectory();
            } else {
                /**deleteFile:删除FTP服务器上的文件
                 * 1)只用于删除文件而不是目录,删除成功时,返回 true
                 * 2)删除目录时无效,方法返回 false
                 * 3)待删除文件不存在时,删除失败,返回 false
                 * */
                boolean deleteFlag = ftpClient.deleteFile(deleteFiles);
                if (deleteFlag) {
                    System.out.println(">>>>>删除服务器文件成功****" + deleteFiles);
                } else {
                    System.out.println(">>>>>删除服务器文件失败****" + deleteFiles);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 使用完毕,应该及时关闭连接
     * 终止 ftp 传输
     * 断开 ftp 连接
     *
     * @param ftpClient
     * @return
     */
    public static FTPClient closeFTPConnect(FTPClient ftpClient) {
        try {
            if (ftpClient != null && ftpClient.isConnected()) {
                ftpClient.abort();
                ftpClient.disconnect();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return ftpClient;
    }

}

3.FTP测试类

package com.lee.demo.ftp;

import com.lee.demo.ftp.util.FtpUtil;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.ws.rs.core.Application;
import java.io.*;

import static com.lee.demo.ftp.util.FtpUtil.*;


/**
 * 创建人:Jimmy
 * 创建时间:2019年11月11日23:23
 * 描述:FTP测试类
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class FtpTest {

    /**
     * FTP查询文件信息
     * @throws IOException
     */
    @Test
    public void ftpQuery() throws IOException {
        String ftpHost = "192.168.0.222";
        int ftpPort = 21;
        String ftpUserName = "test";
        String ftpPassword = "123456";
        String ftpEncoding = "gbk";

        System.out.println("=========应用启动==========");
        FTPClient ftpClient = FtpUtil.connectFtpServer(ftpHost, ftpPort, ftpUserName, ftpPassword, ftpEncoding);
        System.out.println("FTP 连接是否成功:" + ftpClient.isConnected());
        System.out.println("FTP 连接是否有效:" + ftpClient.isAvailable());

        FTPFile[] ftpFiles = ftpClient.listFiles();
        for (FTPFile ftpFile : ftpFiles) {
            System.out.println("文件名:" + ftpFile.getName());
        }

        closeFTPConnect(ftpClient);
        System.out.println("=========应用关闭==========");
    }

    /**
     * FTP文件上传
     */
    @Test
    public void ftpUpdate() {
        String ftpHost = "192.168.0.222";
        int ftpPort = 21;
        String ftpUserName = "test";
        String ftpPassword = "123456";
        String ftpEncoding = "gbk";
        String localPath = "C:/Users/liqiu/Desktop/pic/1.jpg";

        System.out.println("=========应用启动==========");
        FTPClient ftpClient = FtpUtil.connectFtpServer(ftpHost, ftpPort, ftpUserName, ftpPassword, ftpEncoding);

        uploadFiles(ftpClient, new File(localPath));

        closeFTPConnect(ftpClient);
        System.out.println("=========应用关闭==========");

    }

    /**
     * FTP单文件下载
     */
    @Test
    public void ftpSingleDownload() {
        String ftpHost = "192.168.0.222";
        int ftpPort = 21;
        String ftpUserName = "test";
        String ftpPassword = "123456";
        String ftpEncoding = "gbk";

        System.out.println("=========应用启动==========");
        FTPClient ftpClient = FtpUtil.connectFtpServer(ftpHost, ftpPort, ftpUserName, ftpPassword, ftpEncoding);
        downloadSingleFile(ftpClient, "F:/ftp/ftpDownload", "/home/test/1.jpg");
        closeFTPConnect(ftpClient);
        System.out.println("=========应用关闭==========");
    }

    /**
     * 文件源FTP服务器转存文件至目标FTP服务器(存本地==》再上传==》删除本地文件)
     * @throws Exception
     */
    @Test
    public void task() throws Exception {
        System.out.println("=========连接文件源FTP服务器==========");
        String sourceFtpHost = "192.168.0.222";
        int sourceFtpPort = 21;
        String sourceFtpUserName = "test";
        String sourceFtpPassword = "123456";
        String sourceFtpEncoding = "gbk";
        String localPath = "F:/ftp/ftpDownload";
        String remotePath = "/home/test/1.jpg";
        FTPClient sourceFtpClient = FtpUtil.connectFtpServer(sourceFtpHost, sourceFtpPort, sourceFtpUserName, sourceFtpPassword, sourceFtpEncoding);

        System.out.println("=========获取文件==========");
        downloadSingleFile(sourceFtpClient, localPath, remotePath);
        closeFTPConnect(sourceFtpClient);

        System.out.println("=========连接转存目标FTP服务器==========");
        String targetFtpHost = "192.168.0.102";
        int targetFtpPort = 21;
        String targetFtpUserName = "ftpUser";
        String targetFtpPassword = "123456";
        String targetFtpEncoding = "gbk";
        FTPClient targetFtpClient = FtpUtil.connectFtpServer(targetFtpHost, targetFtpPort, targetFtpUserName, targetFtpPassword, targetFtpEncoding);

        System.out.println("=========转存文件至目标FTP服务器==========");
        String localFinalPath = localPath + remotePath;
        uploadFiles(targetFtpClient, new File(localFinalPath));
        closeFTPConnect(targetFtpClient);

        //删除本地临时文件
        File folder= new File(localPath + "/" + remotePath.split("/")[1]);
        delFolder(folder);
    }

    private void delFolder(File folder) throws Exception {
        if (!folder.exists()) {
            throw new Exception("文件不存在");
        }
        File[] files = folder.listFiles();
        if (files != null) {
            for (File file : files) {
                if (file.isDirectory()) {
                    //递归直到目录下没有文件
                    delFolder(file);
                } else {
                    //删除
                    file.delete();
                }
            }
        }
        //删除
        folder.delete();
    }

    /**
     * FTP下载文件夹
     * @throws Exception
     */
    @Test
    public void downloadDir() throws Exception {
        System.out.println("=========连接文件源FTP服务器==========");
        String sourceFtpHost = "192.168.0.222";
        int sourceFtpPort = 21;
        String sourceFtpUserName = "test";
        String sourceFtpPassword = "123456";
        String sourceFtpEncoding = "gbk";
        String localDirectoryPath = "F:/ftp/ftpDownload/";
        String remoteDirectory = "/home/test/dir001/";
        FTPClient sourceFtpClient = FtpUtil.connectFtpServer(sourceFtpHost, sourceFtpPort, sourceFtpUserName, sourceFtpPassword, sourceFtpEncoding);

        downLoadDirectory(sourceFtpClient, localDirectoryPath, remoteDirectory);
        closeFTPConnect(sourceFtpClient);
    }

    /**
     * 上传文件夹
     * @throws Exception
     */
    @Test
    public void uploadDir() throws Exception {
        System.out.println("=========连接文件源FTP服务器==========");
        String sourceFtpHost = "192.168.0.222";
        int sourceFtpPort = 21;
        String sourceFtpUserName = "test";
        String sourceFtpPassword = "123456";
        String sourceFtpEncoding = "gbk";
        String localDirectory = "F:/ftp/ftpDownload/dir001/";
        String remoteDirectoryPath = "/home/test/";
        FTPClient sourceFtpClient = FtpUtil.connectFtpServer(sourceFtpHost, sourceFtpPort, sourceFtpUserName, sourceFtpPassword, sourceFtpEncoding);

        uploadDirectory(sourceFtpClient, localDirectory, remoteDirectoryPath);
        closeFTPConnect(sourceFtpClient);

    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值