import it.mindflavor.ftp.*;
import java.io.*;
import java.net.*;

public class TestFTP {
	
public static void main(String ag[]) {
	final String URL = "ftp.honors.unr.edu";
	try {
		
		FTPConnection conn = new FTPConnection(URL);
		FTPAsker asker = new FTPAsker(conn);
		WaitForCodeHandler handler = new WaitForCodeHandler(false, "TestFTP.Main()");
		conn.addFTPHandling(handler);
		conn.logon();
		if(conn.isOk()) {
			asker.anonymousLogon("trymail@foo.it");
				
			asker.changeDir("pub");
			asker.changeDir("games");
			asker.changeDir("windows");
									
			RemoteFile[] rf = asker.listCurrentDir();
			for(int i=0; i<rf.length; i++)
				System.out.println((i+1) + ") " + rf[i]);
			
			asker.setType(true);
			BufferedReader bin = new BufferedReader(new InputStreamReader(asker.retrive(rf[0])));
			FileOutputStream fout = new FileOutputStream(new File("message.txt"));
			int i = 0;
			while( (i = bin.read()) != -1)
				fout.write(i);
			
			bin.close();
			fout.close();
			System.out.println("File \"" + rf[0].getName() + "\" richiamato con successo.");
			System.exit(0);
		} else {
			System.out.println("Non posso accedere al sito.");
		}
	} catch(UnknownHostException e) {
		System.err.println("Non posso accedere al sito FTP("+URL+").\nErrore: " + e);
	} catch(IOException e) {
		System.err.println("Errore di connessione: " + e);
	}
}
	
}