ftp 递归下载文件 下载过了就不在下载了

Posted 大数据的未来

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ftp 递归下载文件 下载过了就不在下载了相关的知识,希望对你有一定的参考价值。


package com.bailiangroup.bigdata.utils;

import java.io.IOException;
import java.net.SocketException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;

public class FTPUtil
	private static  Log logger = LogFactory.getLog(FTPUtil.class);
	public static FTPClient getFTPClient(String ftpHost, String ftpUserName,String ftpPassword, int ftpPort)   
        FTPClient ftpClient = null;  
        try   
            ftpClient = new FTPClient();  
            ftpClient.connect(ftpHost, ftpPort);// 连接FTP服务器  
            ftpClient.login(ftpUserName, ftpPassword);// 登陆FTP服务器  
            if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode()))   
                logger.info("未连接到FTP,用户名或密码错误。");  
                ftpClient.disconnect();  
             else   
                logger.info("FTP连接成功。");  
              
         catch (SocketException e)   
            e.printStackTrace();  
            logger.info("FTP的IP地址可能错误,请正确配置。");  
         catch (IOException e)   
            e.printStackTrace();  
            logger.info("FTP的端口错误,请正确配置。");  
          
        return ftpClient;  
      


package com.bailiangroup.bigdata.utils;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;

public class FtpFileDownloadUtil
	private static Log log = LogFactory.getLog(FtpFileDownloadUtil.class);
	private String ftpHost;
	private String ftpUserName;
	private String ftpPassword;
	private int ftpPort;
	private String remotePath;
	private String localPath;
	private String fileName;
	FTPClient ftpClient = null;

	public FtpFileDownloadUtil( String ftpHost, String ftpUserName, String ftpPassword, int ftpPort,String remotePath,String localPath)
		this.ftpHost = ftpHost;
		this.ftpUserName = ftpUserName;
		this.ftpPassword = ftpPassword;
		this.ftpPort = ftpPort;
		this.remotePath = remotePath;
		this.localPath = localPath;
	
	
	
	public  void download()
		try 
			//获得ftp连接
			ftpClient = FTPUtil.getFTPClient(ftpHost, ftpUserName, ftpPassword, ftpPort);
			if (ftpClient == null) 
				log.error("连接远程ftp服务失败");
				return;
			
			ftpClient.enterLocalPassiveMode();
			ftpClient.setFileType(ftpClient.BINARY_FILE_TYPE);
			//从ftp中下载远程目录文件
			downLoadFile(ftpClient, remotePath, localPath);
			
			
		catch(Exception e)
		 	
		finally
			//关闭ftp
			if (ftpClient != null && ftpClient.isConnected()) 
				try 
					ftpClient.disconnect();
				 catch (IOException e) 
				
			
			
			
		
	
	
	

	/** 
	    * 递归下载文件 
	    *  
	    * @param ftp 
	    * @param remote 
	    * @param local 
	    *  
	    */  
	    
	   private void downLoadFile(FTPClient ftp, String remote, String local)  
	     
		   
	       try  
	         
	  
	           // 转到指定下载目录  
	    	  
	           ftp.changeWorkingDirectory(remote); 
        	   //System.out.println("remote:" +remote);

	           FTPFile[] files = ftp.listFiles(); 
	           
	           for (FTPFile file : files)  
	             
	        	  // System.out.println(file.getName());
	        	 //if(file.getName().contains("2015-11-24"))
	        		   
	        		   if (file.isDirectory())
	        		     
	        			   downLoadFile(ftp, remote + file.getName() + "/", local + "/" + file.getName() + "/");
	        		     
	        		   else  
	        		     
	        			   
	        			   File localFile = new File(local +  File.separator + file.getName());  
	        			   
	        			   if (!localFile.getParentFile().exists())  
	        			     
	        				   localFile.getParentFile().mkdirs(); 
	        			    
	        			   
	        			  if(!localFile.exists())
	        				  
	        				  OutputStream is = new FileOutputStream(localFile);  
	        				  // 下载文件  
	        				  ftp.retrieveFile(file.getName(), is);  
	        				  is.close();  
	        			  
	        			   
  
	        		     
	        	   
	               
	               
	               
	          //   
	  
	         
	       catch (Exception e)  
	         
	           e.printStackTrace();  
	         
	  
	     
	 



		public String getFtpHost() 
			return ftpHost;
		


		public void setFtpHost(String ftpHost) 
			this.ftpHost = ftpHost;
		


		public String getFtpUserName() 
			return ftpUserName;
		


		public void setFtpUserName(String ftpUserName) 
			this.ftpUserName = ftpUserName;
		


		public String getFtpPassword() 
			return ftpPassword;
		


		public void setFtpPassword(String ftpPassword) 
			this.ftpPassword = ftpPassword;
		


		public int getFtpPort() 
			return ftpPort;
		


		public void setFtpPort(int ftpPort) 
			this.ftpPort = ftpPort;
		


		public String getRemotePath() 
			return remotePath;
		


		public void setRemotePath(String remotePath) 
			this.remotePath = remotePath;
		


		


		public String getLocalPath() 
			return localPath;
		


		public void setLocalPath(String localPath) 
			this.localPath = localPath;
		


		public String getFileName() 
			return fileName;
		


		public void setFileName(String fileName) 
			this.fileName = fileName;
		


		public FTPClient getFtpClient() 
			return ftpClient;
		


		public void setFtpClient(FTPClient ftpClient) 
			this.ftpClient = ftpClient;
		


	
	
	



以上是关于ftp 递归下载文件 下载过了就不在下载了的主要内容,如果未能解决你的问题,请参考以下文章

java爬虫下载FTP网站目录文件

java爬虫下载FTP网站目录文件

利用MFC的CFtpConnection类实现递归下载文件夹

利用MFC的CFtpConnection类实现递归下载文件夹

wget下载ftp协议的文件,限制下载文件大小。

使用 FluentFTP 从 FTP 并发下载多个文件,最大值