2016-04-14 24 views
0

我試圖使用FTPClient我的代碼下載從服務器下載的zip文件是Java的FTP客戶端不下載文件

FTPClient ftpClient = new FTPConnection().makeConnection(loc); 

     try { 
      ftpClient.setFileType(FTP.BINARY_FILE_TYPE); 
      success = ftpClient.changeWorkingDirectory(PATH + preset + "/" + file_to_download + offset); 
      System.out.println("Download Path:-" + PATH + preset + "/" + file_to_download + offset); 
      if (!success) { 
       System.out.println("Could not changed the directory to RIBS"); 
       return; 
      } else { 
       System.out.println("Directory changed to RIBS"); 
      } 
      FTPFile[] files = ftpClient.listFiles(); 
      for (FTPFile file : files) { 
       if (file.getName().contains(".zip")) { 
        dfile = file; 
       } 

      } 
      fsize=dfile.getSize(); 
      fileMap.put("build", dfile.getName()); 
      primaryStage = (Stage) ap.getScene().getWindow(); 

      String homePath = System.getProperty("user.home"); 
      File downloadPath = new File(homePath + "\\Buildss\\" + osVer); 
      if (!downloadPath.exists()) { 
       if (downloadPath.mkdirs()) { 
        System.out.println("Directory is created!"); 
       } else { 
        System.out.println("Failed to create directory!"); 
       } 
      } 
      // System.out.println(chosenDir.getAbsolutePath()); 
      filePath = new File(downloadPath + "/" + dfile.getName()); 
      if (filePath.exists()) { 
       System.out.println("File altready exist"); 
       return; 
      } 
      else { 
       fileMap.put("path", filePath.toString()); 
       fileMap.put("kind", "RIBS"); 


       Task downloadTask = new Task<Void>() { 
        @Override 
        public Void call() throws IOException { 
         try { 
          long len = dfile.getSize(); 
          System.out.println("File From Server:::::: " + len); 
          downloadFile = new File(downloadPath + "/" + dfile); 
          outputFile = new FileOutputStream(downloadFile); 
         } catch (FileNotFoundException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
         } 
         ftpClient.sendNoOp(); 
         ftpClient.setConnectTimeout(1000); 
         //ftpClient.retrieveFile(dfile, output); 
        if (ftpClient.retrieveFile(dfile.getName(), outputFile) == true) { 
          downloadButton.setDisable(true); 
          System.out.println("LOCAL FILE LENGTH:-" + downloadFile.length()); 

         } 
         return null; 
        } 
       }; 
       Thread t = new Thread(downloadTask); 
       t.start(); 

但現在如果我去的下載位置我看到這樣的「-rwxrwx --- 1 ftp ftp 513235293 Apr 11 06「但沒有下載文件。這段代碼在幾天前工作文件。

+0

'retrieveFile()'調用後,您可能需要調用'getReplyCode()'來檢查最後一次FTP回覆的回覆代碼。它會給你更多的信息。 –

+0

回覆代碼是226,它表示「在成功處理影響數據連接的前一個客戶端命令後關閉數據連接之前,服務器發送了一個226響應代碼。在大多數情況下,它表示文件傳輸完成」 – user3649361

+0

Can你還添加輸出(sys out)語句?我也會sysout'downloadFile'對象來看看它在寫什麼。 –

回答

0

我相信這個問題是關係到以下

// File dfile 
FTPFile[] files = ftpClient.listFiles(); 
for (FTPFile file : files) { 
    if (file.getName().contains(".zip")) { 
    dfile = file; 
    } 
} 
... 
File downloadPath = new File(homePath + "\\Buildss\\" + osVer); 
... 
// File downloadFile; 
downloadFile = new File(downloadPath + "/" + dfile); 

我認爲dfile簡稱代表的Unix文件File對象。所以downloadPath + "/" + dfile的連接不會返回你所期望的。

您應該使用downloadPath.getName() + "/" + dfile.getName()。但是您需要從dfile.getName()中刪除可能的路徑名。優選的方式是使用java.nio.file.Path對象。