`
yuancihang
  • 浏览: 142204 次
  • 性别: Icon_minigender_1
  • 来自: 洛阳
社区版块
存档分类
最新评论

使用FTP4J上传下载删除文件及文件夹

F# 
阅读更多

   使用ftp4j来操作ftp非常容易, 把我平时积累的FTP工具类拿出来和大家分享. 欢迎大家提出改进意见.

 

1. 依赖库

 ftp4j-1.5.jar

2. 共用方法

   上传下载方法需要引用的方法:

public static URL newURL(URL parentUrl, String child)throws MalformedURLException {
        String path = parentUrl.getPath();
        if(!path.endsWith("/")){
            path += "/";
        }
        path += child;
        return new URL(parentUrl, path);
    }

3. 下载文件

public static void download(URL url, String username, String password, File file){
        download(url, username, password, "UTF-8", file);
    }
    public static void download(URL url, String username, String password, String encoding, File file){
        FTPClient client = new FTPClient();
        client.setCharset(encoding);
        client.setType(FTPClient.TYPE_BINARY);
        //连接到指定的FTP服务器(域名或IP) 不指定端口,则使用默认端口21
        try {
            int port = url.getPort();
            if(port < 1){
                port = 21;
            }
            client.connect(url.getHost(), port);
            //登录验证
            client.login(username, password);
//            client.changeDirectory(url.getPath());
            client.download(url.getFile(), file);
            client.logout();
            //安全退出
            client.disconnect(true);
        } catch (Exception e) {
            logger.warn(e.getMessage(), e);
        }
    }

4. 下载文件夹

public static void downloadFolder(URL url, String username, String password, File folder){
        downloadFolder(url, username, password, "UTF-8", folder);
    }
    public static void downloadFolder(URL url, String username, String password, String encoding, File folder){
        FTPClient client = new FTPClient();
        client.setCharset(encoding);
        client.setType(FTPClient.TYPE_BINARY);
        //连接到指定的FTP服务器(域名或IP) 不指定端口,则使用默认端口21
        try {
            int port = url.getPort();
            if(port < 1){
                port = 21;
            }
            client.connect(url.getHost(), port);
            //登录验证
            client.login(username, password);
            downloadFolder(client, url, folder);
            client.logout();
            //安全退出
            client.disconnect(true);
        } catch (Exception e) {
            logger.warn(e.getMessage(), e);
        }
    }
    private static void downloadFolder(FTPClient client, URL url, File folder)throws Exception{
        client.changeDirectory(url.getPath());
        FTPFile[] ftpFiles = client.list();
        if(!folder.exists()){
            folder.mkdirs();
        }
        for(FTPFile ftpFile : ftpFiles){
            String name = ftpFile.getName();
            if(ftpFile.getType() == FTPFile.TYPE_FILE){
                client.changeDirectory(url.getPath());
                File f = new File(folder, name);
                client.download(name, f);
            }else if(ftpFile.getType() == FTPFile.TYPE_DIRECTORY){
                downloadFolder(client, newURL(url, name), new File(folder, name));
            }
        }
    }

5. 上传文件

public static void upload(URL url, String username, String password, File file){
        upload(url, username, password, "UTF-8", file);
    }
    public static void upload(URL url, String username, String password, String encoding, File file){
        FTPClient client = new FTPClient();
        client.setCharset(encoding);
        client.setType(FTPClient.TYPE_BINARY);
        //连接到指定的FTP服务器(域名或IP) 不指定端口,则使用默认端口21
        try {
            int port = url.getPort();
            if(port < 1){
                port = 21;
            }
            client.connect(url.getHost(), port);
            //登录验证
            client.login(username, password);
            client.changeDirectory(url.getPath());
            client.upload(file);
            client.logout();
            //安全退出
            client.disconnect(true);
        } catch (Exception e) {
            logger.warn(e.getMessage(), e);
        }
    }

6. 上传文件夹

public static void uploadFolder(URL url, String username, String password, File folder){
        uploadFolder(url, username, password, "UTF-8", folder);
    }
    public static void uploadFolder(URL url, String username, String password, String encoding, File folder){
        FTPClient client = new FTPClient();
        client.setCharset(encoding);
        client.setType(FTPClient.TYPE_BINARY);
        //连接到指定的FTP服务器(域名或IP) 不指定端口,则使用默认端口21
        try {
            int port = url.getPort();
            if(port < 1){
                port = 21;
            }
            client.connect(url.getHost(), port);
            //登录验证
            client.login(username, password);
            client.changeDirectory(url.getPath());
            String[] files = folder.list();
            for(String file : files){
                File f = new File(folder, file);
                if(f.isDirectory()){
                    uploadFolder(client, url, f);
                }else{
                    client.changeDirectory(url.getPath());
                    client.upload(f);
                }
            }
            client.logout();
            //安全退出
            client.disconnect(true);
        } catch (Exception e) {
            logger.warn(e.getMessage(), e);
        }
    }
    private static void uploadFolder(FTPClient client, URL parentUrl, File folder)throws Exception{
        client.changeDirectory(parentUrl.getPath());
        if(!existsFtpDir(client, folder.getName())){
            client.createDirectory(folder.getName());
        }
        client.changeDirectory(folder.getName());
       
        String[] fileList = folder.list();
        if(fileList == null){
            return ;
        }
       
        for(String file : fileList){
            File f = new File(folder, file);
            if(f.isDirectory()){
                uploadFolder(client, newURL(parentUrl, folder.getName()), f);
            }else if(f.isFile()){
                client.changeDirectory(parentUrl.getPath());
                client.changeDirectory(folder.getName());
                client.upload(f);
            }
        }
    }
   
    private static boolean existsFtpDir(FTPClient client, String dir)throws Exception{
        FTPFile[] ftpFiles = client.list();
        for(FTPFile ftpFile : ftpFiles){
            if((ftpFile.getType() == FTPFile.TYPE_DIRECTORY) && (dir.equals(ftpFile.getName()))){
                return true;
            }
        }
        return false;
    }

7. 判断是否存在文件或文件夹

public static boolean exists(URL url, String username, String password){
        return exists(url, username, password, "UTF-8");
       
    }
    public static boolean exists(URL url, String username, String password, String encoding){
        FTPClient client = new FTPClient();
        client.setCharset(encoding);
        client.setType(FTPClient.TYPE_BINARY);
        try {
            int port = url.getPort();
            if(port < 1){
                port = 21;
            }
            String[] welcome = client.connect(url.getHost(), port);
            logger.info("welcome = " + welcome.length);
            //登录验证
            client.login(username, password);
            String path = url.getPath();
            logger.info("path = " + path);
            if(path.indexOf(".") != -1){//文件
                int index = path.lastIndexOf("/");
                String p = path.substring(0, index);
                String f = path.substring(index + 1);
                logger.info("p = " + p + ", f = " + f);
                client.changeDirectory(p);
                FTPFile[] ftpFiles = client.list();
                for(FTPFile ftpFile : ftpFiles){
                    if((ftpFile.getType() == FTPFile.TYPE_FILE) && (ftpFile.getName().equals(f))){
                        return true;
                    }
                }
                return false;
            }else{//文件夹
                client.changeDirectory(path);
            }
            client.logout();
            //安全退出
            client.disconnect(true);
        } catch (Exception e) {
            logger.warn(e.getMessage(), e);
            return false;
        }
       
        return true;
    }

8. FTP服务器之间复制文件夹

public static void copyFolder(URL sourceUrl, String sourceUsername, String sourcePassword, String sourceEncoding, URL destUrl, String destUsername, String destPassword, String destEncoding, String tmpDir)throws IOException{
        downloadFolder(sourceUrl, sourceUsername, sourcePassword, sourceEncoding, new File(tmpDir));
        uploadFolder(destUrl, destUsername, destPassword, destEncoding, new File(tmpDir));
        FileUtil.deleteFolder(tmpDir);
    }
    public static void copyFolder(URL sourceUrl, String sourceUsername, String sourcePassword, URL destUrl, String destUsername, String destPassword, String tmpDir)throws IOException{
        copyFolder(sourceUrl, sourceUsername, sourcePassword, "UTF-8", destUrl, destUsername, destPassword, "UTF-8", tmpDir);
    }

9.删除文件

public static void delete(URL url, String username, String password){
        delete(url, username, password, "UTF-8");
    }
    public static void delete(URL url, String username, String password, String encoding){
        FTPClient client = new FTPClient();
        client.setCharset(encoding);
        client.setType(FTPClient.TYPE_BINARY);
        //连接到指定的FTP服务器(域名或IP) 不指定端口,则使用默认端口21
        try {
            int port = url.getPort();
            if(port < 1){
                port = 21;
            }
            client.connect(url.getHost(), port);
            //登录验证
            client.login(username, password);
            client.deleteFile(url.getPath());
            client.logout();
            //安全退出
            client.disconnect(true);
        } catch (Exception e) {
            logger.warn(e.getMessage(), e);
        }
    }

 

10. 简单用法

uploadFolder(new URL("ftp://localhost:21/test/"), "admin", "123", new File("d:/test"));
downloadFolder(new URL("ftp://localhost:21/test/"), "admin", "123", new File("d:/dbtest"));

分享到:
评论
2 楼 yuancihang 2011-02-20  
可以支持中文的, 你把错误贴出来
1 楼 brook19 2011-02-16  
这个可以支持中文的吗,为什么我一上传中文文件名的文件就报错?请问有什么解决办法!

相关推荐

Global site tag (gtag.js) - Google Analytics