我正在使用Apache Commons上传大文件,但传输速度只是使用WinSCP通过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);
}
}
}发布于 2013-02-02 19:07:56
增加缓冲区大小:
client.setBufferSize(1024000);发布于 2013-01-20 05:21:53
使用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();发布于 2013-12-11 10:50:58
在Java1.7和Commons NET3.2中有一个已知的问题,这个错误是https://issues.apache.org/jira/browse/NET-493
如果运行这些版本,我建议首先升级到Commons Net 3.3。显然,3.4也修复了更多的性能问题。
https://stackoverflow.com/questions/11572588
复制相似问题