从ftp下载指定文件到指定的目录

package ftp;


import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;  
import java.io.OutputStream;
import java.io.PrintWriter;  
import java.util.ArrayList;  
import java.util.Date;
import java.util.Iterator;  


import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.net.PrintCommandListener;  
import org.apache.commons.net.ftp.FTPClient;  
import org.apache.commons.net.ftp.FTPFile;  
import org.apache.commons.net.ftp.FTPReply;  


/** 
 * 列出FTP服务器上指定目录下面的所有文件 
 * @author BenZhou mailto:zhouzb@qq.com 
 * 原文地址:http://zhouzaibao.iteye.com/blog/362866  
 */  
public class FTPListAllFiles {  
    //private static Logger logger = Logger.getLogger(FTPListAllFiles.class);  
    public FTPClient ftp;  
    public ArrayList<String> arFiles;  
      
    /** 
     * 重载构造函数 
     * @param isPrintCommmand 是否打印与FTPServer的交互命令 
     */  
    public FTPListAllFiles(boolean isPrintCommmand){  
        ftp = new FTPClient();  
        arFiles = new ArrayList<String>();  
        if(isPrintCommmand){  
            ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));  
        }  
    }  
      
    /** 
     * 登陆FTP服务器 
     * @param host FTPServer IP地址 
     * @param port FTPServer 端口 
     * @param username FTPServer 登陆用户名 
     * @param password FTPServer 登陆密码 
     * @return 是否登录成功 
     * @throws IOException 
     */  
    public boolean login(String host,int port,String username,String password) throws IOException{  
        this.ftp.connect(host,port);  
        if(FTPReply.isPositiveCompletion(this.ftp.getReplyCode())){  
            if(this.ftp.login(username, password)){  
                this.ftp.setControlEncoding("GBK");  
                return true;  
            }  
        }  
        if(this.ftp.isConnected()){  
            this.ftp.disconnect();  
        }  
        return false;  
    }  
      
    /** 
     * 关闭数据链接 
     * @throws IOException 
     */  
    public void disConnection() throws IOException{  
        if(this.ftp.isConnected()){  
            this.ftp.disconnect();  
        }  
    }  
      
    /** 
     * 递归遍历出目录下面所有文件 
     * @param pathName 需要遍历的目录,必须以"/"开始和结束 
     * @throws IOException 
     */  
    public void List(String pathName) throws IOException{  
        if(pathName.startsWith("/")&&pathName.endsWith("/")){  
            String directory = pathName;  
            //更换目录到当前目录  
            this.ftp.changeWorkingDirectory(directory);  
            FTPFile[] files = this.ftp.listFiles();  
            for(FTPFile file:files){  
                if(file.isFile()){  
                    arFiles.add(directory+file.getName());  
                }else if(file.isDirectory()){  
                    List(directory+file.getName()+"/");  
                }  
            }  
        }  
    }  
      
    /** 
     * 递归遍历目录下面指定的文件名 
     * @param pathName 需要遍历的目录,必须以"/"开始和结束 
     * @param ext 文件的扩展名 
     * @throws IOException  
     */  
    public void List(String pathName,String ext,String path) throws IOException{  
        if(pathName.startsWith("/")&&pathName.endsWith("/")){  
            String directory = pathName;  
            //更换目录到当前目录  
            this.ftp.changeWorkingDirectory(directory);  
            FTPFile[] files = this.ftp.listFiles();  
            for(FTPFile file:files){  
                if(file.isFile()){  
                    if(file.getName().startsWith(ext)){  
                        File localFile = new File( path+ File.separatorChar + file.getName());
                        if(localFile.exists()){ 
                            System.out.println( file.getName()+"已存在"); 
                        }else{ 
                            arFiles.add(directory+file.getName()); 
                            OutputStream os = new FileOutputStream(localFile);  
                            this.ftp.retrieveFile(file.getName(), os);  
                        }
                    }  
                }else if(file.isDirectory()){  
                    List(directory+file.getName()+"/",ext,"");  
                }  
            }  
        }  
    }  
    public static void main(String[] args) throws IOException {  
        FTPListAllFiles f = new FTPListAllFiles(true);
        String value = DateFormatUtils.format(new Date(), "yyyyMMdd");
        String filename ="user"+value;
        if(f.login("10.10.1.77", 21, "ceshi", "123456")){  
            f.List("/BOSS/ADP/add/",filename,"D:/ftpdownload/add");  
            f.List("/BOSS/ADP/all/",filename,"D:/ftpdownload/all");  


        }  
        f.disConnection();  
        Iterator<String> it = f.arFiles.iterator();  
        while(it.hasNext()){  
            //logger.info(it.next());  
        System.out.println(it.next());
        }  
    }  
}  
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值