为什么在java 7中ftp上传速度慢

Ada*_*Law 10 java ftp performance

我想问一下是否有人知道FTP的任何Java 7问题?我已经使用了Sun Net和Apache Commons Net库,并且都在Java 6上按预期执行.但是当我将我的开发环境(Eclipse)切换到1.7时,相同的操作执行速度非常慢(大约4.5到8KB/s),这些是本地主机服务器和局域网内的另一台服务器.

我尝试过缓冲流,字节到字节传输,关闭Nagle算法,并使用Apache便捷方法storeFile(),后者最终执行以加速localhost,但再次减速到远程服务器上的爬行.我还设置所有机器关闭状态FTP过滤.

    InputStream is = null;
    OutputStream os = null;
    try {
        is = new BufferedInputStream(prepareInputStream(data));
        os = new BufferedOutputStream(prepareOutputStream(data));
        if (is == null || os == null) {
            log.error("Can't build connection");
            return;
        }

        byte[] buf = new byte[4096];
        int c = 1;

        while (c > 0) {
            c = is.read(buf);
            if (c > 0)
            os.write(buf, 0, c);
            data.incrCurrentPosition();
            fireStateChanged(data);
        }
        data.incrCurrentPosition();
    } catch (IOException e) {
        log.error(e.getMessage(), e);
        setEnabled(false);  
    } catch (Exception e) {
        log.error(e.getMessage(), e);
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (os != null) {
            try {
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
Run Code Online (Sandbox Code Playgroud)

可以看出,这是非常标准的实现代码.同样,在Java 6中,事情非常快.在Java 7中,Sun和Apache Commons库的速度降低了10到20倍.使用像FileZilla这样的FTP客户端确认FTP运行正常,所以我认为它确实与Java 7有关.我尽可能在线挖掘任何提及问题但是,我看到的事情大多是关于Java 7和Windows 7防火墙发生冲突.

提前感谢您提出的任何见解.

Ada*_*Law 13

我找到了一种修复方法,至少足以使Java 7中的内容正常运行.我是通过使用FTPClient的setBufferSize(0)来完成的.不幸的是,我认为Sun的Java 7的Sun Net实现中没有类似的方法.这并不重要,因为我对Apache Commons Net非常满意.希望Oracle能够及时解决这个问题.


M-D*_*M-D 11

请检查您当前的缓冲区大小:

ftpClient.getBufferSize();
Run Code Online (Sandbox Code Playgroud)

如果您尚未将其设置为其他值,则为零(0).因此,将其设置为更高的值:

ftpClient.setBufferSize(1048576);//1024*1024
Run Code Online (Sandbox Code Playgroud)

您可以像以前一样检查其当前值:

ftpClient.getBufferSize();
Run Code Online (Sandbox Code Playgroud)

顺便说一句,接受的答案setBufferSize(0)对我不起作用.我使用最新版本的Apache commons,因此该解决方案可能适用于早期版本.如果将缓冲区大小设置为零,则当前版本将不会更改.


Vol*_*man 5

Commons Net 3.2存在已知的性能问题,已在3.3快照中修复,您可以从此处获取:

https://repository.apache.org/content/groups/snapshots/commons-net/commons-net/3.3-SNAPSHOT/

虽然setBufferSize(0)似乎是一个有效的解决方法,但最好使用快照获得正确的修复 - 如果你可以使用快照;)