/*
                     * 上传目录
                     * client:FTP客户端对象
                     * parentUrl:父节点URL
                     * file:目录
                     * del:
                     */
                    private void uploadFolder(FTPClient client, URL parentUrl, File file,  
                            boolean del) throws Exception {  
                        client.changeDirectory(parentUrl.getPath());  
                        String dir = file.getName(); // 当前目录名称  
                        URL url = getURL(parentUrl, dir);  
                        if (!exists(client, url.getPath())) { // 判断当前目录是否存在  
                            client.createDirectory(dir); // 创建目录  
                        }  
                        client.changeDirectory(dir);  
                        File[] files = file.listFiles(); // 获取当前文件夹所有文件及目录  
                        for (int i = 0; i < files.length; i++) {  
                            file = files[i];  
                            if (file.isDirectory()) { // 如果是目录,则递归上传  
                                uploadFolder(client, url, file, del);  
                            } else { // 如果是文件,直接上传  
                                client.changeDirectory(url.getPath());  
                                client.upload(file);  
                                if (del) { // 删除源文件  
                                    file.delete();  
                                }  
                            }  
                        }  
                    }
                    
                    
                    
                    /*
                     * 删除目录
                     * client:FTP客户端对象
                     * url:FTP URL
                     */
                    private void deleteFolder(FTPClient client, URL url) throws Exception {  
                        String path = url.getPath();  
                        client.changeDirectory(path);  
                        FTPFile[] files = client.list();  
                        String name = null;  
                        for (FTPFile file : files) {  
                            name = file.getName();  
                            // 排除隐藏目录  
                            if (".".equals(name) || "..".equals(name)) {  
                                continue;  
                            }  
                            if (file.getType() == FTPFile.TYPE_DIRECTORY) { // 递归删除子目录  
                                deleteFolder(client, getURL(url, file.getName()));  
                            } else if (file.getType() == FTPFile.TYPE_FILE) { // 删除文件  
                                client.deleteFile(file.getName());  
                            }  
                        }  
                        client.changeDirectoryUp();// 反回上一级目录
                        client.deleteDirectory(url.getPath()); // 删除当前目录  
                    }  
                    
                    
                    
                    /**
                     * 下载文件夹
                     *  
                     * @param client
                     *            FTP客户端对象
                     * @param url
                     *            FTP URL
                     * @param localDir
                     *            本地存储目录
                     * @throws Exception
                     */  
                    private void downloadFolder(FTPClient client, URL url, String localDir)  
                            throws Exception {  
                        String path = url.getPath();  
                        client.changeDirectory(path);  
                        // 在本地创建当前下载的文件夹  
                        File folder = new File(localDir + "/" + new File(path).getName());  
                        if (!folder.exists()) {  
                            folder.mkdirs();  
                        }  
                        localDir = folder.getAbsolutePath();  
                        FTPFile[] files = client.list();  
                        String name = null;  
                        for (FTPFile file : files) {  
                            name = file.getName();  
                            // 排除隐藏目录  
                            if (".".equals(name) || "..".equals(name)) {  
                                continue;  
                            }  
                            if (file.getType() == FTPFile.TYPE_DIRECTORY) { // 递归下载子目录  
                                downloadFolder(client, getURL(url, file.getName()), localDir);  
                            } else if (file.getType() == FTPFile.TYPE_FILE) { // 下载文件  
                                client.download(name, new File(localDir + "/" + name));  
                            }  
                        }  
                        client.changeDirectoryUp();  
                    }  
                    
                    
                    /**
                     * 上传文件或目录
                     * @param dir目标文件
                     * @param del是否删除源文件,默认为false
                     * @param files文件或目录对象数组
                     * @param del:是否删除源文件,true删除,false不删除
                     * @throws Exception
                     */
                    public void upload(String dir, boolean del, File... files) throws Exception {  
                        if (StringUtils.isEmpty(dir) || StringUtils.isEmpty(files)) {  
                            return;  
                        }  
                        FTPClient client = null;  
                        try {  
                            client = getClient();  
                            mkdirs(client, dir); // 创建文件  
                            for (File file : files) {  
                                if (file.isDirectory()) { // 上传目录  
                                    uploadFolder(client, getURL(dir), file, del);  
                                } else {  
                                    client.upload(file); // 上传文件  
                                    if (del) { // 为true删除源文件  
                                        file.delete();  
                                    }  
                                }  
                            }  
                        } finally {  
                            logout(client);  
                        }  
                    }  
                    
                    /**
                     * 上传文件或目录
                     * @param dir目标文件
                     * @param files文件或目录对象数组
                     * @throws Exception
                     */
                    public void upload(String dir, File... files) throws Exception {  
                        upload(dir, false, files);  
                    }
                    
                    
                    
                    /**
                     * 删除文件或目录
                     * @param dirs
                     * @throws Exception
                     */
                    public void delete(String... dirs) throws Exception {  
                        if (StringUtils.isEmpty(dirs)) {  
                            return;  
                        }  
                        FTPClient client = null;  
                        try {  
                            client = getClient();  
                            int type = -1;  
                            for (String dir : dirs) {  
                                client.changeDirectory("/"); // 切换至根目录  
                                type = getFileType(client, dir); // 获取当前类型  
                                if (type == 0) { // 删除文件  
                                    client.deleteFile(dir);  
                                } else if (type == 1) { // 删除目录  
                                    deleteFolder(client, getURL(dir));  
                                }  
                            }  
                        } finally {  
                            logout(client);  
                        }  
                    }  
                    
                    
                    
                    
                    
                     /**
                     * 下载文件或目录
                     *  
                     * @param localDir
                     *            本地存储目录
                     * @param dirs
                     *            文件或者目录
                     * @throws Exception
                     */  
                    public void download(String localDir, String... dirs) throws Exception {  
                        if (StringUtils.isEmpty(dirs)) {  
                            return;  
                        }  
                        FTPClient client = null;  
                        try {  
                            client = getClient();  
                            File folder = new File(localDir);  
                            if (!folder.exists()) { // 如果本地文件夹不存在,则创建  
                                folder.mkdirs();  
                            }  
                            int type = -1;  
                            String localPath = null;  
                            for (String dir : dirs) {  
                                client.changeDirectory("/"); // 切换至根目录  
                                type = getFileType(client, dir); // 获取当前类型  
                                if (type == 0) { // 文件  
                                    localPath = localDir + "/" + new File(dir).getName();  
                                    client.download(dir, new File(localPath));  
                                } else if (type == 1) { // 目录  
                                    downloadFolder(client, getURL(dir), localDir);  
                                }  
                            }  
                        } finally {  
                            logout(client);  
                        }  
                    }
                 QQ截图20181015143900.png