Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.1k views
in Technique[技术] by (71.8m points)

performance - why is ftp upload slow in java 7

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.

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I found a fix of sorts, at least enough to get things running normally in Java 7. I did it by using FTPClient's setBufferSize(0); Unfortunately, I don't think there's a similar method in Sun's Java 7's Sun Net implementation. Not that it matters to me as I'm quite pleased with Apache Commons Net. Hopefully, Oracle will get to the bottom of this in due time.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...