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?
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);
}
}
}
use the outputStream method, and transfer with a buffer.
Increase the buffer size:
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
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.
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... :)