2012-04-05 16 views
0

本当に「put」がファイルを宛先に置くことに成功したかどうかを知りたい。何らかの理由でファイルが宛先に配置されていない場合(おそらく、スペース制約などの宛先サーバの問題のために)、私はそれを知る必要があります。sun.net.ftp.FtpClientでftpの成功/失敗をトラップする方法は?

コード:

private static boolean putFile(String m_sLocalFile, FtpClient m_client) { 
      boolean success = false; 
    int BUFFER_SIZE = 10240; 
    if (m_sLocalFile.length() == 0) { 
     System.out.println("Please enter file name"); 
    } 
    byte[] buffer = new byte[BUFFER_SIZE]; 
    try { 
     File f = new File(m_sLocalFile); 
     int size = (int) f.length(); 
     System.out.println("File " + m_sLocalFile + ": " + size + " bytes"); 
     System.out.println(size); 
     FileInputStream in = new FileInputStream(m_sLocalFile); 
     OutputStream out = m_client.put(f.getName()); 

     int counter = 0; 
     while (true) { 
      int bytes = in.read(buffer); 
      if (bytes < 0) 
       break; 
      out.write(buffer, 0, bytes); 
      counter += bytes; 
      System.out.println(counter); 
     } 

     out.close(); 
     in.close(); 
    } catch (Exception ex) { 
     System.out.println("Error: " + ex.toString()); 
    } 
      return success; 
} 

答えて

0

私はそれがIOExceptionを投げることを期待します。あなたはそれが信じられない理由がありますか?しかし、そのクラスを直接使用するべきではありません。を呼び出した後に、ftp:URLとそのURLConnectionクラスを使用してI/Oを行う必要があります。

+0

'in.close();'の後に成功すれば成功だと思いますか?もちろん@itro – itro

+0

です。 – EJP

関連する問題