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

时间:2022-11-11
本文介绍了为什么在 java 7 中 ftp 上传速度很慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

I wanted to ask if anyone knows about any Java 7 issues with FTP? I've used both the Sun Net and Apache Commons Net libraries and both perform as expected on Java 6. But when I switch my dev environment (Eclipse) to 1.7, the same operations perform really slow (about 4.5 to 8KB/s), and these are to localhost servers and another server within the LAN.

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

I've tried buffered streams, byte-to-byte transfer, turning the Nagle Algorithm off, and using the Apache convenience method storeFile(), with the latter finally performing to speed on localhost but slowing down again to a crawl on a remote server. I also set all machines to turn off stateful FTP filtering.

    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 防火墙冲突.

As can be seen, this is pretty standard implementation code. Again, in Java 6, things zip by really quick. In Java 7, it slows down by a factor of 10 to 20 for both the Sun and Apache Commons libraries. Using an FTP client like FileZilla confirms that FTP is functioning normally, so I think it really has something to do with Java 7. I dug as far as I could online for any mention of a problem but, mostly, the things I saw were about the Java 7 and Windows 7 firewall conflict.

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

Thanks in advance for any insight given.

推荐答案

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

Please check what your current buffer size is with :

ftpClient.getBufferSize();

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

If you haven't already set it to something else, that will be zero (0). So, set it to a higher value :

ftpClient.setBufferSize(1048576);//1024*1024

你可以像以前一样检查它的当前值:

You can check its current value as before :

ftpClient.getBufferSize();

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

By the way, the accepted answer, setBufferSize(0), did not work for me. I use the latest version of Apache commons, so probably that solution worked with earlier versions. If you set buffer size to zero, there will be no change with the current version.

这篇关于为什么在 java 7 中 ftp 上传速度很慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:使用 Java DSL 成功进行 ftp 传输后移动文件 下一篇:FTPClient.listFiles 不工作

相关文章

最新文章