1

I have tried to use the Apache Commons Net 3.5 to save files from a FTP-Server. Most times my code will work fine and the speed at which the files are downloaded is really fast. There are several FTP-Servers I try to save files from. Some of them are really slow when I try to download files. Even small 1000 Byte files need up to 30 seconds. When using programs like WinSCP, the transfer rate is fine. I am not sure, but it may only occur on ProFTPD-Servers. At least all of the ProFTPD-Servers I have tried are slow while all noneProFTPD-Servers were fine. (small samplesize, might be a coincidence)

I can't change any FTP-Server side specific configuration files, because I have no access to them. There is only the possibility to change my Java-Code, which hopefully will be enough because other programs like WinSCP don't have this issue.

Here is my Code. (removed some validation- / handle- code)

Connecting:

public boolean connect() {
    ftp = new FTPClient();
    ftp.setAutodetectUTF8( true );
    try {
        ftp.connect(host,port);
        int reply = ftp.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            ftp.disconnect();
            //handle that...
        }

        ftp.login(username, password);
        ftp.setFileType(FTP.BINARY_FILE_TYPE);
        ftp.enterLocalPassiveMode();
        ftp.setBufferSize(1024*1024);

        return true;
    } catch (SocketException e) {
        //handle..
    } catch (IOException e) {
        //handle...
    } 

    return false;
}

Downloading:

public void downloadAll(String base, String downBase){
    try {

        ftp.changeWorkingDirectory(base);
        System.out.println("Current directory is " +  ftp.printWorkingDirectory());
        FTPFile[] ftpFiles = ftp.listFiles();


        if (ftpFiles != null && ftpFiles.length > 0) {

            for (FTPFile file : ftpFiles) {
                if (!file.isFile()) {
                    continue;
                }
                System.out.println("File is " + file.getName());


                File f = new File(downBase);
                f.mkdirs();
                OutputStream output = new BufferedOutputStream(
                     new FileOutputStream(downBase+ file.getName()));

                InputStream inputStream = ftp.retrieveFileStream(file.getName());
                byte[] bytesArray = new byte[1024*1024];
                int bytesRead = -1;
                while ((bytesRead = inputStream.read(bytesArray)) != -1) {
                    output.write(bytesArray, 0, bytesRead);
                }

                ftp.completePendingCommand();

                output.close();
                inputStream.close();
            }
        }
    } catch (IOException e) {
        // handle...
    }
}

After downloading everything I am closing the FTP-Client.

My research didn't helped me at all. There are a few that say, they have slow FTP-download-times. But in my case this is only on specific servers. On most servers it's really good!

I tried different buffer-sizes.

I hope you can help me with this problem. Feel free to ask for any information you need!

Knappe

EDIT:

output = new FileOutputStream(downBase+ file.getName());        
ftp.retrieveFile(file.getName(), output);
output.close();

Seems to fix that issue! I used that way to retrieve files in my first version of the program, but I had other issues with that. I don't remember exactly which problems :-D.

I still don't have any clue, why the other code isn't working properly. Thank you!

Knappe
  • 11
  • 3
  • Sounds like there are server-side configurations which may be affecting your times; not having access to those configurations makes the list of things you can do from the client side, for those servers, much smaller. – Castaglia Apr 03 '17 at 20:37
  • Yeah, but those seem only be set on ProFTPD-Servers. Also WinSCP has no problems with the file download. There has to be something I can do, I just don't know what. Any ideas? – Knappe Apr 03 '17 at 20:43
  • Analyze the difference, in terms of FTP commands, between WinSCP and your client code? – Castaglia Apr 03 '17 at 20:44

0 Answers0