FileDocCategorySizeDatePackage
FTPExample.javaAPI DocExample2849Wed May 18 09:39:02 BST 2005com.discursive.jccook.net

FTPExample

public class FTPExample extends Object

Fields Summary
Constructors Summary
Methods Summary
public static voidmain(java.lang.String[] args)

		new FTPExample().start();
	
private voidsecondExample()

        FTPClient client = new FTPClient();
        client.connect( "ftp.ibiblio.org" );
        client.login( "anonymous", "" );
        
        String remoteDir = "/pub/mirrors/apache/jakarta/ecs/binaries";
        
        FTPFile[] remoteFiles = client.listFiles( remoteDir );
        
        System.out.println( "Files in " + remoteDir );
        
        for (int i = 0; i < remoteFiles.length; i++) {
        	String name = remoteFiles[i].getName();
        	long length = remoteFiles[i].getSize();
        	String readableLength = FileUtils.byteCountToDisplaySize( length );
        
            System.out.println( name + ":\t\t" + readableLength );
        }
        
        client.disconnect();
    
public voidstart()


		FTPClient client = new FTPClient();
		OutputStream outStream = null;

		try {
            client.connect( "ftp.ibiblio.org" );
			client.login( "anonymous", "" );
		
			String remoteFile = "/pub/micro/commodore/schematics/computers/c64/c64bus.gif";
			outStream = new FileOutputStream( "c64bus.gif" );

			client.retrieveFile( remoteFile, outStream );
		} catch(IOException ioe) {
			System.out.println( "Error communicating with FTP server." );
		} finally {
			IOUtils.closeQuietly( outStream );
			try {
                client.disconnect();
            } catch (IOException e) {
            	System.out.println( "Problem disconnecting from FTP server" );
            }
		}


        secondExample();