Speed up Apache Commons FTPClient transfer

2019-02-12 15:17发布

问题:

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);
        }
    }         
}

回答1:

Increase the buffer size:

client.setBufferSize(1024000);


回答2:

use the outputStream method, and transfer with a buffer.

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();


回答3:

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.



回答4:

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... :)