繁体   English   中英

加速Apache Commons FTPClient传输

[英]Speed up Apache Commons FTPClient transfer

我使用Apache Commons FTPClient上传大文件,但传输速度只是使用WinSCP通过FTP传输速度的一小部分。 我怎样才能加快转移?

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

增加缓冲区大小:

client.setBufferSize(1024000);

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

有一个已知的Java 1.7和Commons Net 3.2,该bug是https://issues.apache.org/jira/browse/NET-493

如果运行这些版本,我建议升级到Commons Net 3.3作为第一步。 显然3.4修复了更多的性能问题。

如果你使用ftp.setbuffersize(0)会更好; 如果你使用0作为缓冲区大小,它将采用无限缓冲区大小。 显然你的交易会加速......我个人经历过......一切都很好...... :)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM