java 加速 Apache Commons FTPClient 传输
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11572588/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Speed up Apache Commons FTPClient transfer
提问by Tommy Ng
I am using Apache Commons FTPClient to upload large files, but the transfer speed is only a fraction of transfer speed using WinSCP via FTP. How can I speed up my transfer?
我正在使用 Apache Commons FTPClient 上传大文件,但传输速度只是通过 FTP 使用 WinSCP 传输速度的一小部分。如何加快传输速度?
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);
}
}
}
回答by marden
Increase the buffer size:
增加缓冲区大小:
client.setBufferSize(1024000);
回答by Mike Jouwstra
use the outputStream method, and transfer with a buffer.
使用 outputStream 方法,并使用缓冲区传输。
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();
回答by KCD
There is a known issued with Java 1.7 and Commons Net 3.2, the bug is https://issues.apache.org/jira/browse/NET-493
有一个已知的 Java 1.7 和 Commons Net 3.2 发布,错误是https://issues.apache.org/jira/browse/NET-493
If running these versions I'd suggest the upgrade to Commons Net 3.3 as the first step. Apparently 3.4 fixes more performance issues too.
如果运行这些版本,我建议第一步升级到 Commons Net 3.3。显然 3.4 也修复了更多的性能问题。
回答by Nandakishore
It would be better if you use ftp.setbuffersize(0); if you use 0 as your buffersize , it will take as infinite buffer size. obviously ur transaction will get speeded up... I personally experienced it.. all the best... :)
如果你使用 ftp.setbuffersize(0) 会更好;如果您使用 0 作为您的 buffersize ,它将采用无限缓冲区大小。显然你的交易会得到加速......我个人经历过......一切顺利...... :)