FTPClient download file 2 methods

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.

链接到sFTP客户端谷歌浏览器应用程序 访问您的本地/远程FTP服务器(包括您的NAS驱动器),本地服务器,VPS,专用服务器,云服务器或共享主机。 我们现在已经发布了适用于MAC,WINDOWS和LINUX的本地应用程序,请访问我们的网站: http://www.sftpclient.io/download ------------------------------------------ sFTP客户端是简单的,它是建立在谷歌浏览器/ Chrome操作系统打包的应用程序界面,使应用程序快速和响应。其中一些功能包括一个FTP / SFTP帐户管理器,用于存储和管理最常用和最喜欢的FTP / SFTP连接,只需点击一下,文件/文件夹队列即可查看当前正在上传/下载的项目,强大的文本编辑器(所以你甚至不需要额外的程序来修改你的代码),等等... 看看下面的sFTP客户端应用程序的所有功能。 特征: ========= - 标准的FTP连接 - SSH over File Transfer Protocol(sFTP)连接 - 用于SSH连接的权限密钥文件(SSH密钥 - RSA) - FTP / SFTP被动模式 - 连接到远程(外部)和本地(内部)FTP / SFTP / SSH服务器。 - 更改文件/文件夹权限(通过复选框或值:例如777) - 上传/下载多个文件和文件夹 - 快速连接 - 编辑器选项:选项卡式文件,自定义 - 拖放文件/文件夹 - 管理FTP / SFTP / SSH账户(使用谷歌浏览器本地存储和安全密码加密存储) - 强大的文本编辑器,语法高亮(保存,自动保存和自动上传功能) - 键盘选择(向上,向下,进入(回车)和退出(退格)的目录,包括搜索能力,通过键盘上键入快速访问文件/文件夹) - 导入帐户:sFTP客户端 - 出口帐户 - 重命名和删除文件 - 创建新的文件/目录 - 刷新本地和远程列表 - 对列进行排序和调整大小 - 多选文件和文件夹 - 按路径浏览本地和远程文件夹 - 本地目录:选择每个连接的默认本地目录,假设为全局 - 快速帐户菜单(一键从您保存的列表中打开FTP / SFTP / SSH帐户连接) - 多个FTP / SFTP / SSH帐户连接(如果您打开了大量连接,则滚动选项卡) - 关闭连接(断开与服务器的连接并删除所有活动) - 控制台日志(显示所有FTP / SFTP / SSH活动日志) - 传输队列(排队的文件和文件夹,失败的文件和文件夹,完成的文件和文件夹) - 新的Google Sockets API - 远程和本地菜单 - 连接并列出UNIX和MS-DOS目录 - 复制网址到剪贴板 - 10最近的连接 - 保持连接 - 主密码登录(保持所有的FTP连接安全,1登录) - 同步浏览 更多的功能和功能来免费未来的更新! 支持语言:English (UK)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值