簡體   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