import java.io.PrintWriter;
import java.io.OutputStreamWriter;
import java.io.FileOutputStream;
import java.io.File;
import java.io.IOException;
import org.gjt.tst.net.ftp.client.*;


public class FtpClient {
    private FtpClientExtraProtocol ftp;
    // This does the actual download
    private static void downloadTheFile()
        throws IOException, InterruptedException {

        File file = new File("test.txt");
        FileOutputStream out = null;
        try {
            out = new FileOutputStream(file);
            ftp.download(out, "test.txt");
        }
        finally {
            if (null != out) {
                out.close();
                out = null;
            }
        }

    }


    public static void main(String[] args)  {

        PrintWriter trace =
            new PrintWriter(new OutputStreamWriter(System.err),true);

        try {
            ftp = new FtpClientExtraProtocol("localhost", trace);
            ftp.setTimeout(2000);
        }
        catch (IOException e) {
            trace.println("Cannot connect to server");
            return;
        }

        ftp.setDataConnectionActiveServer();
        try {
            ftp.user("kirk");
            if (!ftp.isLoggedIn())
                ftp.password("enterprise");
                downloadTestFile(ftp);
        }
        catch (InterruptedException ex) {
            trace.println(ex.getMessage());
        }
        catch (IOException ex) {
            trace.println(ex.getMessage());
        }
        finally {
            try {
                ftp.logout();
                ftp.close();
            } catch (IOException e) {}
        }
    }

