2011-07-15 32 views
3

FTPClientを使用してデータベースサーバーにファイルをアップロードするのは難しいです。ファイルが正常に転送されたことを示していますが、ファイルは空です(サイズ0バイト)。サーバーにファイルをFTPで送信しますが、結果はゼロバイトサイズで到着します

以下は、私が構築に使用したソースコードです。誰でもこの問題を解決できますか?

package Examples; 

import org.apache.commons.net.ftp.*; 
import java.io.FileInputStream; 
import java.io.IOException; 

public class Main { 

    public static void main(String[] args) { 

     FTPClient client = new FTPClient(); 
     FileInputStream fis = null; 

     try { 

      client.connect("server"); 
      client.login("userid", "password"); 
      System.out.print("Message : " + client.getReplyString()); 

      client.changeWorkingDirectory("/loaddata"); 
      System.out.println("Working Directory" + client.printWorkingDirectory()); 

      client.setDefaultPort(22); 
      int f1 = client.getDefaultPort(); 
      boolean f2 = client.setFileType(FTPClient.BINARY_FILE_TYPE); 
      System.out.println("File transfer port no " + f1); 
      System.out.println("FTP server reply ." + client.getReplyString()); 

      String localfile = "c:/Touch.txt"; 
      fis = new FileInputStream(localfile); 
      int lastSlash = localfile.lastIndexOf('/'); 
      String filename = localfile.substring(lastSlash+1); 
      System.out.println("file : "+fis); 

      client.setFileTransferMode(2); 
      System.out.println("Flag reply ." + client.getReplyString()); 

      boolean flag = client.storeFile(filename,fis); 

      System.out.println("Flag reply ." + client.getReplyString()); 

      if (flag) { 
       System.out.println("Successfully uploaded the file"); 
      } else { 
       System.out.println("Not able to upload the file"); 
      } 

      fis.close(); 
      client.logout(); 
      System.out.println("Logout ." + client.getReplyString()); 

     } catch (Exception e) { 
      System.out.println("Exception " + e); 
     } finally { 
      if (client.isConnected()) { 
       try { 
        client.disconnect(); 
        System.out.println("Server Disconnected." + client.getReplyString()); 
       } catch (IOException ioe) { 
        // do nothing 
       } 
      } 
     } 
    } 
} 
+0

同様の問題がここに掲載されていますhttp://stackoverflow.com/questions/6718338/0-kb-file-created-once-ftp-is-done-in-java –

答えて

1

他のいくつかのFTPClient-の質問を見て、私はその理由は、バージョン3.0でbug in the Apache Commons-NET library(うちFTPClientがコンポーネントである)だと思います。

新しいバージョンをインストールしてください(3.0.1ではこのバグが修正されています)。

+0

Iを3.0.9を使用していますが、同じことが起こり、モトローラだけが無視します。 – VenoM

1

client.changeWorkingDirectory( "/ loaddata");を削除しました。 それ以外の場合はすべてそのまま残し、成功しました。回線が問題になることはありますか? より再び私が使用コモンズ3.1

関連する問題