首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >加快Apache Commons FTPClient传输速度

加快Apache Commons FTPClient传输速度
EN

Stack Overflow用户
提问于 2012-07-20 11:45:36
回答 4查看 14.4K关注 0票数 12

我正在使用Apache Commons上传大文件,但传输速度只是使用WinSCP通过FTPClient传输速度的一小部分。如何加快传输速度?

代码语言:javascript
复制
    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);
        }
    }         
}
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2013-02-02 19:07:56

增加缓冲区大小:

代码语言:javascript
复制
client.setBufferSize(1024000);
票数 33
EN

Stack Overflow用户

发布于 2013-01-20 05:21:53

使用outputStream方法,并使用缓冲区进行传输。

代码语言:javascript
复制
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();
票数 2
EN

Stack Overflow用户

发布于 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也修复了更多的性能问题。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11572588

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档