Downloading from FTP servers in Java

Some months ago I needed a program which did the following:

– Connectsto an FTP host

– Check everything inside the remote folder

– Download all the files

– Delete all the files

– Wait a minute until the next connection

I found a very tiny program which did it ok but it has a problem, it’s always hanging and the information I had to receive was not properly updated, so I took the decision to make myself a program to do the same. The decision of using Java as the programming language was easy, but my Java skills were not as good as a year ago.

Luckily I found a great class called JvFTP (Yes! You have guessed: Java FTP) that works. After some tries this was the result:

import cz.dhl.io.*;
import cz.dhl.ftp.*;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class FTPClass {
    public static final int    BUFFER_SIZE = 10240;
    
    public static void ftpdownload(String host, String user, String pass) {
        /*
         * Creates the connection to the FTP Server using the given parameters
         */
        FtpConnect cn = FtpConnect.newConnect(“ftp://” + host);
        cn.setUserName(user);
        cn.setPassWord(pass);
        Ftp cl = new Ftp();

        try {
            /*
             * Connects to the host
             */
            cl.connect(cn);

            /*
             * Gets current path
             */
            CoFile dir = new FtpFile(cl.pwd(), cl);

            /*
             * Gets the list of files inside the directory
             */
            CoFile fls[] = dir.listCoFiles();
            if (fls != null)
                for (int n = 0; n < fls.length; n++) {
                    /*
                     * Prints the name of the file. If it’s a directory it’ll be discarded
                     */
                    System.out.println(
                        fls[n].getName() + (fls[n].isDirectory() ? “/” : “”));
                    String pathFile = fls[n].getName() + (fls[n].isDirectory() ? “/” : “”);
                    /*
                     * Creates the InputStream from the FTP connection
                     */
                    FtpFile file = new FtpFile(pathFile, cl);              
                    InputStream in = file.getInputStream();
                    /*
                     * Creates the OutputStream to the file in the local disk
                     */
                    OutputStream out= new FileOutputStream(fls[n].getName());
                    byte[] buffer= new byte[BUFFER_SIZE];
                    /*
                     * Reads and writes…
                     */
                    while (true) {
                      int k= in.read(buffer);
                      if (k < 0)
                        break;
                      out.write(buffer, 0, k);
                    }
                    in.close();
                    out.close();
                }
            
        } catch (IOException e) {
            System.out.println(e);
        } finally { /* disconnect from server
              * this must be always run */
            cl.disconnect();
        }
    }
    public static void main(String args[]) {
        ftpdownload(“myhost”, “myuser”, “mypass”);
    }
}