FTPClient download file 2 methods

原创 2013年12月04日 17:53:23

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.

相关文章推荐

Struts2学习笔记(十七) 文件下载(File Download)

前面我们刚刚学完文件上传,那么我们就接着来看和文件上传相对应的文件下载吧。对于文件上传而言,文件下载实现起来要简单的多。通常我们可以直接将一个超链接的地址指向我们想要给用户下载的资源即可。但是如果这些...

FtpClient read write create upload download

package com.chinasoft.ge.util;   import java.io.ByteArrayOutputStream; import java.io.File; impo...
  • lisaiyu
  • lisaiyu
  • 2013年03月15日 12:22
  • 656

ftpclient upload and download----^^very well

4.import java.io.File; 5.import java.io.FileInputStream; 6.import java.io.FileOutputStream; ...

url download File

  • 2016年04月29日 13:11
  • 60KB
  • 下载

test file, please do not download

  • 2009年12月27日 10:05
  • 67KB
  • 下载

Use DropzondJS and Spring to upload and download file

Saving user files in a web application is pretty much a necessity in many cases be it images, videos...

android download file

  • 2012年10月31日 22:01
  • 50KB
  • 下载

Unity 3D本地发布WebPlayer版时Failed to download data file解决方案(IIS 6/7)

遇到这个问题就是指Web服务器并没有支持这种*.unity3d文件类型。需要做的是在Web服务器中添加MIME类型: IIS 7 及以上版本: 在功能视图的IIS选项卡中:  ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:FTPClient download file 2 methods
举报原因:
原因补充:

(最多只允许输入30个字)