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

标签 java performance ftp

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

我已经尝试过缓冲流、字节到字节传输、关闭 Nagle 算法以及使用 Apache 便利方法 storeFile(),后者最终在本地主机上执行速度很快,但在远程服务器。我还设置了所有机器以关闭状态 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();
            }
        }

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

提前感谢您提供的任何见解。

最佳答案

我找到了某种修复方法,至少足以让事情在 Java 7 中正常运行。我是通过使用 FTPClient 的 setBufferSize(0); 实现的,不幸的是,我认为没有类似的方法Sun 的 Java 7 的 Sun Net 实现中的方法。这对我来说并不重要,因为我对 Apache Commons Net 非常满意。希望 Oracle 会在适当的时候查明此事。

关于java - 为什么在 java 7 中 ftp 上传速度慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14000341/

相关文章:

java - 映射任务中长正则表达式的最佳选择是什么?

javascript - jquery中switch和if中为什么推荐使用else if

java - Isabelle:Isabelle2013-2 版本的性能问题

ftp - 有没有办法将文件同步到未安装 rsync 的远程位置?

java - 在 Spring-JPA 应用程序中使用 JAXB 访问对象

java - 为游戏添加声音。需要帮助

windows - wscript 和 cscript 之间的区别

powershell - 使用 PowerShell 测试 FTP 连接

java - Emacs 和 Java 更改传播和错误通知

algorithm - 提高性能(超时错误)