简体   繁体   中英

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

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

I have just implemented a sort of following code.

I discovered that if you use compressed streams , you cannot rely on the transferred bytes reported by the listener, because the server can wait for further data in order to decode the previous received blocks.

Hovewer, even if streams are plains, in some cases of lost connection you still cannot rely on the total transferred bytes as reported by the listener. So, I finally realized that the best way is to ask server how many bytes it received.

In my template, the temporal redundancy is more general and involves the control connection with the FTP server. You could limit the while loop to the data connection, ie the upload.

 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 */ } } } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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