热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

加速ApacheCommonsFTPClient传输。-SpeedupApacheCommonsFTPClienttransfer

IamusingApacheCommonsFTPClienttouploadlargefiles,butthetransferspeedisonlyafraction

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上传大型文件,但是使用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);
        }
    }         
}

4 个解决方案

#1


28  

Increase the buffer size:

增加缓冲区大小:

client.setBufferSize(1024000);

#2


2  

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

#3


1  

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, bug是https://www.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也修复了更多的性能问题。

#4


0  

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作为缓冲大小,那么它将作为无限缓冲区大小。很明显,你的交易将会加速……我个人经历过. .愿一切都好!:)


推荐阅读
author-avatar
我家的表妹
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有