关闭

FTPClient download file 2 methods

599人阅读 评论(0) 收藏 举报
分类:

FTPClient download file demo:

        String server = "127.0.0.1";        
        int port = 21;        
        String user = "aaa";        
        String pass = "aaa";         
        FTPClient ftpClient = new FTPClient();        
        try {
        	ftpClient.connect(server, port);            
        	ftpClient.login(user, pass);            
        	ftpClient.enterLocalPassiveMode();            
        	ftpClient.setFileType(FTP.BINARY_FILE_TYPE);             
        	// APPROACH #1: using retrieveFile(String, OutputStream)            
        	String remoteFile1 = "/tmp/test.txt";            
        	File downloadFile1 = new File("E:/Downloads/test.txt");            
        	OutputStream outputStream1 = new BufferedOutputStream(new FileOutputStream(downloadFile1));            
        	boolean success = ftpClient.retrieveFile(remoteFile1, outputStream1);            
        	outputStream1.close();             
        	if (success) {                
        		System.out.println("File #1 has been downloaded successfully.");            
        	}             
        	// APPROACH #2: using InputStream retrieveFileStream(String)            
        	String remoteFile2 = "/tmp/test_bak.txt";            
        	File downloadFile2 = new File("E:/Downloads/test_bak.txt");            
        	OutputStream outputStream2 = new BufferedOutputStream(new FileOutputStream(downloadFile2));            
        	InputStream inputStream = ftpClient.retrieveFileStream(remoteFile2); 
        	        	
        	byte[] bytesArray = new byte[4096];            
        	int bytesRead = -1;            
        	while ((bytesRead = inputStream.read(bytesArray)) != -1) {                
        		outputStream2.write(bytesArray, 0, bytesRead);            
        	}             
        	outputStream2.close();            
        	inputStream.close();         
//        	success = ftpClient.completePendingCommand();            
//        	if (success) {                
        		System.out.println("File #2 has been downloaded successfully.");            
//        	}            
        	} catch (IOException ex) {            
        		System.out.println("Error: " + ex.getMessage());            
        		ex.printStackTrace();        
        	} finally {            
        			try {                
        				if (ftpClient.isConnected()) {                    
        					ftpClient.logout();                    
        					ftpClient.disconnect();                
        					}            
        				} catch (IOException ex) {                
        					ex.printStackTrace();            
        				}    
        	}


 attention:

public boolean completePendingCommand()
                               throws IOException
There are a few FTPClient methods that do not complete the entire sequence of FTP commands to complete a transaction.  These commands require some action by the programmer after the reception of a positive intermediate command.  After the programmer's code completes its actions, it must call this method to receive the completion reply from the server and verify the success of the entire transaction.

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:329223次
    • 积分:4729
    • 等级:
    • 排名:第6377名
    • 原创:156篇
    • 转载:54篇
    • 译文:2篇
    • 评论:31条
    文章分类
    最新评论