2013-05-01 54 views
0

我正在嘗試創建一個ftp下載器,它將從最後讀取文件的位置重新啓動。 我將爲此存儲一些元數據。但是,在測試時,我正在踢出客戶端,並斷開服務器。但手柄沒有進入例外代碼:FTP連接中斷不會拋出異常

package fileresumes; 

/** 
* 
* @author agarwall 
*/ 
import java.io.File; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 
import java.util.Calendar; 
import org.apache.commons.net.ftp.FTPClient; 
import org.apache.commons.net.ftp.FTPConnectionClosedException; 
import org.apache.commons.net.ftp.FTPFile; 

public class FileRes { 

    public static void main(String[] args) { 

     FTPClient client = new FTPClient(); 
     FileOutputStream fos = null; 
     int totalBytesRead = 0; 

     try { 
      client.connect("localhost"); 
      client.login("anonymous", ""); 
      // The remote filename to be downloaded. 
      String filename = "testing.txt"; 
      fos = new FileOutputStream(filename); 

      boolean append = false; 
      int offset = 0; 
      long last_modified = 0; 
      int size = 0; 

      //long ro = client.getRestartOffset(); 
      //ro = client.getRestartOffset(); 
      //Download file from FTP server; 

      final File file = new File("C:/users/deadman/Desktop/", "testing.txt"); 

      if (file.exists()) { 
       last_modified = file.lastModified();  // lastModified() returns the milliseconds since 1970-01-01 
       append = true; 
       // Read offset from meta-data file 
       size = (int) file.length(); 
       offset = size; 
      } 

      //Setting the offset to resume download 
      client.setRestartOffset(offset); 
      InputStream inputFileStream; 

      inputFileStream = client.retrieveFileStream("/large.txt"); 
      int bytesRead = 0; 

      try { 

       OutputStream out = new FileOutputStream(file, append); 
       final byte[] bytes = new byte[1024]; 

       while ((bytesRead = inputFileStream.read(bytes)) != -1) { 
        out.write(bytes, 0, bytesRead); 
        totalBytesRead += bytesRead; 
       } 
       inputFileStream.close(); 
       out.flush(); 

       int get_reply_code = client.getReplyCode(); 

       System.out.println(get_reply_code); 

      } catch (IOException e) { 

       // I want my metadata to be updated here . 

       System.out.println("IOException"); 

      } catch (RuntimeException e) { 
       // I want my metadata to be updated here . 
       System.out.println("Runtime Exception "); 
      } finally { 
       try { 

        int get_reply_code = client.getReplyCode(); 
        System.out.println(get_reply_code); 

        if (fos != null) { 
         fos.close(); 
        } 
        client.disconnect(); 
        System.out.println("finish"); 
       } catch (IOException e) { 
       } 
      } 
     } catch (Exception e) { 
     } 
    } 
} 

任何人都可以幫助我在連接斷開的情況下如何處理這裏的異常。

回答

-1

我一直在嘗試做類似的事情,到目前爲止我唯一能做的就是嘗試另一個命令。我還沒有進行任何編碼轉換,但是對於我來說,如果我登錄並且有超時,被踢等,如果我嘗試更改文件夾(或類似的東西),它會拋出FTPConnectionClosedException。我環顧了谷歌,並在這裏發佈了一個問題,看看是否有更好的方法,但到目前爲止還沒有聽到任何消息。