使用FTP4J恢复上传进度,并获取上传的百分比

时间:2011-04-11 22:09:05

标签: java ftp

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

1 个答案:

答案 0 :(得分:0)

我刚刚实现了以下代码。

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

Hovewer,即使流是平原,在某些连接丢失的情况下,您仍然不能依赖听众报告的总传输字节数。所以,我终于意识到最好的方法是询问服务器收到多少字节。

在我的模板中,时间冗余更为通用,涉及与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 */ } 
   }  
}

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