说明:jar包版本ftp4j-1.7.2.jar

一、创建项目
    项目名称:ftpdemo
二、添加jar包
    1.在项目中创建lib目录
        /lib
    2.在lib目录下添加jar包
        ftp4j-1.7.2.jar
        junit-4.4.jar        
三、添加属性文件
    1.在项目中创建conf目录
        /conf
    2.在conf目录添加属性文件
        属性文件名称:ftpinfo.properties
        属性文件内容:
        ftp_server_address=ftp://192.168.3.200:21
        ftp_server_username=teacher
        ftp_server_password=*******
四、创建工具类
    1.在src下创建包
        包名:cn.jbit
    2.在包下创建创建工具类
        1.字符串处理工具类
            类名:StringUtils.java
            类内容:
            public class StringUtils {
            
                public static boolean isEmpty(String dir) {
                    if (null != dir || !"".equals(dir)) {
                        return false;
                    }
                    return true;
                }
            
                public static boolean isEmpty(File[] files) {
                    if (null != files) {
                        return false;
                    }
                    return true;
                }
            
                public static boolean isEmpty(String[] dirs) {
                    if (null != dirs) {
                        return false;
                    }
                    return true;
                }
            }
        2.加载属性文件工具类
            类名:PropUtils.java
            类内容:
                /**
                 * 属性文件工具类
                 * @author dengdashuai
                 *
                 */
                public class PropUtils {
                    private static Properties properties = new Properties();
                    private PropUtils(){}
                    static{
                        try {
                            properties.load(PropUtils.class.getClassLoader().getResourceAsStream("ftpinfo.properties"));
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    
                    public static String getString(String key){
                        return properties.getProperty(key);
                    }
                }
        3.操作FTP工具类
            类名:StringUtils.java
            类内容:
                public class FTPUtils {
                    private static FTPUtils ftp;
                    // FTP服务地址
                    private static String ADDRESS;
                    // FTP登录用户名
                    private static String USERNAME;
                    // FTP登录密码
                    private static String PASSWORD;
                
                    // 无参构造
                    private FTPUtils() {
                    }
                
                    /*
                     * 加载
                     *     ftp地址
                     *     ftp登录名
                     *     ftp登录密码
                     */
                    static {
                        ADDRESS = PropUtils.getString("ftp_server_address");
                        USERNAME = PropUtils.getString("ftp_server_username");
                        PASSWORD = PropUtils.getString("ftp_server_password");
                    }
                
                    /**
                     * 实例化对象
                     *
                     * @return FTPUtils
                     */
                    public static FTPUtils getInstance() {
                        if (ftp == null) {
                            ftp = new FTPUtils();
                        }
                        return ftp;
                    }
                
                    /*
                     * 获取FTP客户端对象
                     */
                    private FTPClient getClient() throws Exception {
                        // 创建FTP客户端对象
                        FTPClient client = new FTPClient();
                        // 设置编码
                        client.setCharset("utf-8");
                        // 二进制
                        client.setType(FTPClient.TYPE_BINARY);
                        // 创建URL对象
                        URL url = new URL(FTPUtils.ADDRESS);
                        // 获取端口
                        int port = url.getPort() < 1 ? 21 : url.getPort();
                        // 建立连接
                        client.connect(url.getHost(), port);
                        // 登录
                        client.login(FTPUtils.USERNAME, FTPUtils.PASSWORD);
                        // 客户端对象
                        return client;
                    }
                
                    /*
                     * 注销客户端连接
                     * client:FTP客户端对象
                     */
                    private void logout(FTPClient client) throws Exception {
                        if (client != null) {
                            try {
                                // 有些FTP服务器未实现此功能,若未实现则会出错
                                client.logout(); // 退出登录
                            } catch (FTPException fe) {
                            } catch (Exception e) {
                                throw e;
                            } finally {
                                //判断客户端是否连接,如果已经连接服务返回true,否则返回false
                                if (client.isConnected()) {
                                    // 断开连接
                                    client.disconnect(true);
                                }
                            }
                        }
                    }
                
                    /*
                     * 判断当前为文件还是目录
                     * client:FTP客户端对象 dir:目录
                     */
                    private int getFileType(FTPClient client, String dir) throws Exception {
                        FTPFile[] files = null;
                        try {
                            files = client.list(dir);
                        } catch (Exception e) {
                            return -1;
                        }
                        if (files.length > 1) {
                            return FTPFile.TYPE_DIRECTORY;
                        } else if (files.length == 1) {
                            FTPFile f = files[0];
                            if (f.getType() == FTPFile.TYPE_DIRECTORY) {
                                return FTPFile.TYPE_DIRECTORY;
                            }
                            String path = dir + "/" + f.getName();
                            try {
                                int len = client.list(path).length;
                                if (len == 1) {
                                    return FTPFile.TYPE_DIRECTORY;
                                } else {
                                    return FTPFile.TYPE_FILE;
                                }
                            } catch (Exception e) {
                                return FTPFile.TYPE_FILE;
                            }
                        } else {
                            try {
                                client.changeDirectory(dir);
                                client.changeDirectoryUp();
                                return FTPFile.TYPE_DIRECTORY;
                            } catch (Exception e) {
                                return -1;
                            }
                        }
                    }
                
                    /*
                     * 判断文件或目录是否存在 client:FTP客户端对象 dir:目录
                     */
                    private boolean exists(FTPClient client, String dir) throws Exception {
                        return getFileType(client, dir) != -1;
                    }
                
                    /*
                     * 创建目录 client:FTP客户端对象 dir:目录
                     */
                    private void mkdirs(FTPClient client, String dir) throws Exception {
                        if (dir == null) {
                            return;
                        }
                        dir = dir.replace("//", "/");
                        String[] dirs = dir.split("/");
                        for (int i = 0; i < dirs.length; i++) {
                            dir = dirs[i];
                            if (!StringUtils.isEmpty(dir)) {
                                if (!exists(client, dir)) {
                                    client.createDirectory(dir);
                                }
                                client.changeDirectory(dir);
                            }
                        }
                    }
                
                    /*
                     * 获取FTP目录 url:原FTP目录 dir:目录
                     */
                    private URL getURL(URL url, String dir) throws Exception {
                        String path = url.getPath();
                        if (!path.endsWith("/") && !path.endsWith("//")) {
                            path += "/";
                        }
                        dir = dir.replace("//", "/");
                        if (dir.startsWith("/")) {
                            dir = dir.substring(1);
                        }
                        path += dir;
                        return new URL(url, path);
                    }
                
                    /*
                     * 获取FTP目录
                     * dir:目录
                     */
                    private URL getURL(String dir) throws Exception {
                        return getURL(new URL(FTPUtils.ADDRESS), dir);
                    }
                    QQ截图20181015143813.png