2016-09-16 28 views
0

我試圖從服務器下載一個文件,但它得到0字節......FTPClient下載一個0字節的文件

這是我FTPDownload類

public boolean getFile(String filename){ 

     try { 
     FTPClient ftpClient = new FTPClient(); 

      ftpClient.connect(ftpAddress, ftpPort); 
      ftpClient.login(ftpUser, ftpPass); 
      int reply = ftpClient.getReplyCode(); 
      //FTPReply stores a set of constants for FTP reply codes. 
      if (!FTPReply.isPositiveCompletion(reply)) 
      { 
       ftpClient.disconnect(); 
       return false; 

      } 
      ftpClient.enterLocalPassiveMode(); 
      ftpClient.setFileType(FTP.BINARY_FILE_TYPE); 
      ftpClient.setBufferSize(1024*1024); 

      String remoteFile = serverPath + filename; 
      logger.debug("remote file is: "+remoteFile); //correct path 
      File tempFile = new File(downloadDir+"temp.jar"); 
      logger.debug("file will be "+tempFile.toString()); //correctly created 
      OutputStream os = new BufferedOutputStream(new FileOutputStream(tempFile)); 

      ftpClient.retrieveFile(remoteFile, os); 
      os.close(); 

      String completeJarName = downloadDir+jarName; 
      //delete previous file 
      File oldFile = new File(completeJarName); 
      FileUtils.forceDelete(oldFile); 


      //rename 
      File newFile = new File(completeJarName); 
      FileUtils.moveFile(tempFile, newFile); 

      if (ftpClient.isConnected()) { 
       ftpClient.logout(); 
       ftpClient.disconnect(); 

      } 

     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      logger.error("errore ftp", e); 
      return false; 
     } 

     return true; 
    } 

基本上,臨時呸得到創建,然後前面的文件被取消,臨時文件被重命名,但它是0字節...我不明白哪裏出了問題...

+0

我不確定,但嘗試在'ftpClient.retrieveFile'之後但在'os.close()'之前刷新輸出流'os.flush()'。 – Shadov

回答

0

我用apache.common FTP下載。這裏是代碼,你可以嘗試

public class FTPUtils { 
private String hostName = ""; 
private String username = ""; 
private String password = ""; 
private StandardFileSystemManager manager = null; 
FileSystemOptions opts = null; 

public FTPUtils(String hostName, String username, String password) { 
    this.hostName = hostName; 
    this.username = username; 
    this.password = password; 
    manager = new StandardFileSystemManager(); 
} 
    private void initFTPConnection() throws FileSystemException { 
    // Create SFTP options 
    opts = new FileSystemOptions(); 
    // SSH Key checking 
    SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(
    opts, "no"); 
    // Root directory set to user home 
    SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, false); 
    // Timeout is count by Milliseconds 
    SftpFileSystemConfigBuilder.getInstance().setTimeout(opts, 10000); 
} 

public void onUpload(String ftpfolder, String localFilePath, String fileName) { 
    File file = new File(localFilePath); 
    if (!file.exists()) 
    throw new RuntimeException("Error. Local file not found"); 

    try { 
    manager.init(); 
    // Create local file object 
    FileObject localFile = manager.resolveFile(file.getAbsolutePath()); 

    String remoteFilePath = "/" + ftpfolder + "/" + fileName; 
    // Create remote file object 
    FileObject remoteFile = manager.resolveFile(
    createConnectionString(hostName, username, password, 
     remoteFilePath), opts); 
    // Copy local file to sftp server 
    remoteFile.copyFrom(localFile, Selectors.SELECT_SELF); 
    System.out.println("Done"); 
    } catch (Exception e) { 
    // Catch and Show the exception 
    } finally { 
    manager.close(); 

    } 
} 
public static String createConnectionString(String hostName, 
    String username, String password, String remoteFilePath) { 
    return "sftp://" + username + ":" + password + "@" + hostName + "/" 
    + remoteFilePath; 
} 

}

+0

你確定這是下載文件而不是上傳到服務器嗎? – besmart

+0

是的,這是我現有的代碼。我用於我的目的 –

0

的BufferedOutputStream將數據寫入到內部緩衝區,所以你可能需要沖洗 OutputStream的閉幕前:

OutputStream os = new BufferedOutputStream(new FileOutputStream(tempFile)); 

ftpClient.retrieveFile(remoteFile, os); 
os.flush(); 
os.close(); 

另一個祕訣:

  • 總是給Bu提供緩衝區大小(通常是8Kb的倍數)。

  • 在實例化流時始終使用try-with-resources指令,並讓它們自動關閉。

  • 不要留下catch條款沒有適當的處理。 固定爲(如果您希望程序從該例外中恢復),例外情況應該是向上傳播(默認情況下,傳播)。只有日誌不太可能是最好的治療方法。
相關問題