2012-07-20 12 views
10

Apache Commons FTPClientを使用して大きなファイルをアップロードしていますが、転送速度はFTP経由のWinSCPを使用した転送速度のほんの一部です。どうすれば移送をスピードアップできますか?Apache Commons FTPClient転送を高速化する

public boolean upload(String host, String user, String password, String directory, 
     String sourcePath, String filename) throws IOException{ 

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

    try { 
     client.connect(host); 
     client.login(user, password); 
     client.setControlKeepAliveTimeout(500); 

     logger.info("Uploading " + sourcePath); 
     fis = new FileInputStream(sourcePath);   

     // 
     // Store file to server 
     // 
     client.changeWorkingDirectory(directory); 
     client.setFileType(FTP.BINARY_FILE_TYPE); 
     client.storeFile(filename, fis); 
     client.logout(); 
     return true; 
    } catch (IOException e) { 
     logger.error("Error uploading " + filename, e); 
     throw e; 
    } finally { 
     try { 
      if (fis != null) { 
       fis.close(); 
      } 
      client.disconnect(); 

     } catch (IOException e) { 
      logger.error("Error!", e); 
     } 
    }   
} 

答えて

26

バッファサイズ増やします。あなたは ftp.setbuffersize(0)を使用する場合、それは良いだろう

client.setBufferSize(1024000); 
+1

Apacheバージョン3.3は、期待通りに無制限値の代わりにデフォルト値(8192)を使用するため、BufferSizeをゼロに設定するという問題があります。 – Thinhbk

+0

私はバージョン3.2からバージョン3.5に更新しなければならず、それはうまくいきませんでした。たぶん、3.3は動作します。 – JohnyTex

2

出力メソッドを使用し、バッファで転送します。

InputStream inputStream = new FileInputStream(myFile); 
OutputStream outputStream = ftpclient.storeFileStream(remoteFile); 

byte[] bytesIn = new byte[4096]; 
int read = 0; 

while((read = inputStream.read(bytesIn)) != -1) { 
    outputStream.write(bytesIn, 0, read); 
} 

inputStream.close(); 
outputStream.close(); 
+1

この回答は私を大きく助けました。それは大幅に加速しました。 –

0

を。 バッファサイズを0にすると、無限のバッファサイズになります。 明らかウル取引はスピードアップされます...私は個人的にそれを経験した。.. すべての最高の... :)

1

ありのJava 1.7とコモンズネット3.2で発行され知られている、バグがhttps://issues.apache.org/jira/browse/NET-493

です

これらのバージョンを実行している場合は、最初の手順としてCommons Net 3.3へのアップグレードをお勧めします。明らかに3.4ではパフォーマンスの問題も改善されています。

関連する問題