2017-03-14 60 views
1

我試圖在Java中使用FTPClient,並且在檢索單個正確的文件時正常工作。大約需要1秒鐘,然後繼續正確。現在我試圖讓它恢復,如果它得到一個不好的遠程路徑。Java Apache FTPClient在請求第一個文件請求錯誤後請求第二個文件時停止

因此... retrieveFile(badPath) - >花費大約1分鐘告訴我:550文件未找到或權限問題。

我發送另一個retrieveFile(goodPath) - >命令熄滅,150打開BINARY模式數據連接。只停留在那裏!有任何想法嗎?

請注意,兩個正確的請求也正常工作。是否有某種需要從550進行的特殊恢復?

public class FTPFetch 
{ 

// Creating FTP Client instance 
FTPClient ftp = null; 

// Constructor to connect to the FTP Server 
public FTPFetch(String host, int port, String username, String password) throws Exception 
{ 

    ftp = new FTPClient(); 
    ftp.setConnectTimeout(5000); 
    ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out))); 
    int reply; 
    ftp.connect(host, port); 
    reply = ftp.getReplyCode(); 
    if (!FTPReply.isPositiveCompletion(reply)) 
    { 
     ftp.disconnect(); 
     throw new Exception("Exception in connecting to FTP Server"); 
    } 
    ftp.login(username, password); 
    ftp.setFileType(FTP.BINARY_FILE_TYPE); 
    ftp.enterLocalPassiveMode(); 
} 

// Download the FTP File from the FTP Server 
public boolean downloadFTPFile(String source, String destination) 
{ 
    try (FileOutputStream fos = new FileOutputStream(destination)) 
    { 
     if (this.ftp.retrieveFile(source, fos)) 
     { 
      return true; 
     } else 
     { 
      return false; 
     } 
    } catch (IOException e) 
    { 
     return false; 
    } 
} 
// Disconnect the connection to FTP 
public void disconnect() 
{ 
    if (this.ftp.isConnected()) 
    { 
     try 
     { 
      this.ftp.logout(); 
      this.ftp.disconnect(); 
     } catch (IOException f) 
     { 
      // do nothing as file is already saved to server 
     } 
    } 
} 
} 

這裏是我怎麼稱呼它:

if (!ftpobj.downloadFTPFile(s, temp[temp.length - 1])) 
       { 
        if (s.contains("bd0")) 
        { 
         File f2 = new File(temp[temp.length - 1]); 
         errorLog.warn("/bd0 path not found on remote CCU. Attempting /bd5."); 
         ftpobj.downloadFTPFile(s.replace("bd0", "bd5"), temp[temp.length - 1]); 
        } else 
        { 
         File f2 = new File(temp[temp.length - 1]); 
         errorLog.warn("/bd5 path not found on remote CCU. Attempting /bd0."); 
         ftpobj.downloadFTPFile(s.replace("bd5", "bd0"), temp[temp.length - 1]); 
        } 
       } 

我的輸出:

230 User logged in 
TYPE I 
200 Type set to I, binary mode 
PASV 
227 Entering Passive Mode (10,20,40,21,19,117) 
RETR /bd5/CU_Anthony/PN1008TestDir.zip 
550 File "/bd5/CU_Anthony/PN1008TestDir.zip" not found or permission problem 
[WARN ] 03-14-2017 12:48:26 [main] DataAdapterFB1 - /bd5 path not found on  remote CCU. Attempting /bd0. 
PASV 
227 Entering Passive Mode (10,20,40,21,19,117) 
RETR /bd0/CU_Anthony/PN1008TestDir.zip 
150 Opening BINARY mode data connection 

在服務器端,我第二次請求後看到發生了數據連接錯誤,但我沒有跡象表明爲什麼:

0x1890f20 (tFtpdServ0): (3170) data xfer failed: read 4096 bytes, wrote -1 bytes 
0x1890f20 (tFtpdServ0): ftpdCmdSend: <426 Data connection error> 
0x1890f20 (tFtpdServ0): (4136) sent 426 Data connection error 

0x1890f20 (tFtpdServ0): (3413) Closing sock 55 

回答

0

我不確定它爲什麼工作,但將其置於本地活動模式而不是本地被動模式,它工作。

相關問題