繁体   English   中英

使用FTP4J恢复上传进度并获得多少百分比的上传

[英]Use FTP4J to resume upload progress and also get how many percent uploaded

任何人都有如何使用支持恢复的ftp4j上传以及如何显示进度条的示例吗?

我刚刚实现了一种以下代码。

我发现, 如果您使用压缩流 ,则不能依赖侦听器报告的已传输字节,因为服务器可以等待更多数据才能解码先前接收到的块。

因此,即使流是普通的,在某些情况下失去连接​​的情况下,您仍然不能依靠侦听器报告的总传输字节数。 因此,我终于意识到最好的方法是询问服务器它接收了多少字节。

在我的模板中,时间冗余更为笼统,涉及与FTP服务器的控制连接。 您可以将while循环限制为数据连接,即上传。

 FTPClient ftpClient = null; long writtenBytes; boolean isCompletedStartingDelete = false; // Our policy is overwrite at first for (int attempt = 0; attempt < MAX_ATTEMPTS; attempt++) { try { ftpClient = getFTPClient(); configureFtpClient(ftpClient); doLogin(ftpClient); ftpClient.changeDirectory(remoteDirectory); if (!isCompletedStartingDelete) { // Our policy is overwrite at first try { ftpClient.deleteFile(file); isCompletedStartingDelete = true; } catch (FTPException e) { // Maybe you should check if this exception is really thrown for file not existing. isCompletedStartingDelete = true; } } try { writtenBytes = ftpClient.fileSize(fileName); } catch (Exception e) { writtenBytes = 0; } if (ftpClient.isResumeSupported()) { // With this template you also could use APPEND ftpClient.upload(file, writtenBytes, listener); } else { ftpClient.upload(file, listener); } } catch (FTPAbortException e) { // User Aborted operation break; } catch (Exception e) { if (attempt == MAX_ATTEMPTS) { // Or in general lastLoop throw e; } else { // Mask failure // LOG } } finally { if (ftpClient != null && ftpClient.isConnected()) { try { ftpClient.disconnect(); } catch (Throwable t) { /* LOG */ } } } 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM