2013-04-18 61 views
-1

我編寫了這個類(從示例)下載包含在遠程FTP文件夾中的所有文件的標題。它工作的很好,但是當它接近下載文件#146時,它會停止並出現NullPointException。文件#146存在,我可以將其作爲單個文件實際下載。FTP下載停止在文件#146

在該方法中,remotePathLong包含寫在一行中的所有遠程文件夾並以空格字符隔開。

public void downloadHeader(String remotePathLong, String destPath, int bytes) { 

    String remotePath; 
    FTPFile[] fileList; 
    String[] fileNameList; 

    FTPClient ftpClient = new FTPClient(); 
    try { 
     ftpClient.connect(server); 
     ftpClient.login(user, pass); 
     ftpClient.enterLocalPassiveMode(); 
     ftpClient.setFileType(FTP.BINARY_FILE_TYPE); 

     int indice = 0; 
     int iP = 1; 
     File downloadFile; 
     String destFile; 
     String remoteFile; 
     byte[] bytesArray; 
     int bytesRead = -1; 
     while ((remotePath = getPath(remotePathLong, iP)) != null) { 
      System.out.println("Loading file list from the server....."); 
      fileNameList = ftpClient.listNames(remotePath); 
      for (String file : fileNameList) { 
       indice += 1; 
       System.out.println(indice + " - Downloading: " + file); 

       //Select files 
       destFile = destPath.concat(file); 

       downloadFile = new File(destFile); 
       outputStream = new BufferedOutputStream(new FileOutputStream(downloadFile)); 

       //Download remote file (from ftp) 
       remoteFile = remotePath.concat(file); 

       inputStream = ftpClient.retrieveFileStream(remoteFile); 
       bytesArray = new byte[bytes]; 
       bytesRead = inputStream.read(bytesArray); 
       outputStream.write(bytesArray); 

       //Save into file 

       outputStream.close(); 
       inputStream.close(); 
       iP += 1; 
      } 
     } 

    } catch (IOException ex) { 
    } final{ 
     try { 
      if (ftpClient.isConnected()) { 
       ftpClient.logout(); 
       ftpClient.disconnect(); 
      } 
     } 
     catch (IOException ex1) { 
      System.out.println("Error: " + ex1.getMessage()); 
     } 
} 

當它到達bytesRead = inputStream.read(bytesArray)時,在迭代#146它給出錯誤。但如果在同一次迭代中,我重新初始化它的連接。 請問有人有建議嗎?

+0

在迭代#146重新初始化連接? (這裏只是半開玩笑) –

+0

請鏈接到示例? – hd1

+0

如果問題一致,則檢查文件權限 –

回答

0

由於網絡流量或文件大小恰好爲146,您的連接可能會超時。 可以打印第146個文件名並檢查其大小。你也可以增加FTP連接的超時時間

ftpClient.setControlKeepAliveTimeout(300); // set timeout to 5 minutes 
+0

該文件爲hnps2730.00d.Z,文件大小爲308kB。與以前的尺寸沒有什麼不同。無論如何,我認爲連接出現問題。事實上,如果我強迫下載#146下載其中一個以前的文件,它不會這樣做。我試圖把超時也增加到非常大的值,但沒有任何改變。 – user2279697

+0

我會嘗試在每個文件文件傳輸後得到答覆(類似於client.getReplyString())並且還可以netstat -an | grep <您的ftp端口>來查看錯誤發生時coinnection是否還活着? –

+0

因此,我在每個inputStream.read(bytesArray)後面放置了getReplyString(),它總是說: 150-接受的數據連接 150 xx.x kbytes下載 但是對於文件#146它說: 226-文件成功傳輸 2260.000秒(這裏測得),每秒 315.55兆字節但會產生一個NullPointerException Netsat打印: TCP地址:端口碧西:FTP ESTABLISHED – user2279697