10.15. Writing an FTP Client

Problem

You need to write a program to interact with an FTP server.

Solution

Use Commons Net FTPClient to communicate with an FTP server. The following example retrieves the contents of the file c64bus.gif from ftp.ibibilio.org:

import org.apache.commons.io.IOUtils;
import org.apache.commons.net.ftp.FTPClient;

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

try {
    // Connect to the FTP server as anonymous
    client.connect( "ftp.ibiblio.org" );
    client.login( "anonymous", "" );

    String remoteFile = "/pub/micro/commodore/schematics/computers/c64/
                         c64bus.gif";

    // Write the contents of the remote file to a FileOutputStream
    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" );
    }
}

In the previous example, an instance of FTPClient is created; the example then logs on to ftp.ibibio.org as anonymous—with no password—using the connect( ) and login() method on FTPClient. The full path to the remote file c64bus.gif and an OutputStream are passed to retrieveFile() , which then transfers the contents of the c64bus.gif to a local file. Once the file has been retrieved, the FTPClient is disconnected from the server using the disconnect( ) method in a finally block.

Discussion

FTPClient can also be used to list the contents of a directory by passing a directory to the listFiles( ) method. The following example uses FTPClient to print the name and size of every file in the /pub/mirrors/apache/jakarta/ecs/binaries directory on ftp.ibiblio.org:

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;

FTPClient client = new FTPClient( );

// Connect to the FTP server as anonymous
client.connect( "ftp.ibiblio.org" );
client.login( "anonymous", "" );

String remoteDir = "/pub/mirrors/apache/jakarta/ecs/binaries";

// List the contents of the remote directory
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 + ":		" + readableLength );
}
client.disconnect( );

After connecting to ftp.ibiblio.org, this example retrieves an array of FTPFile objects using client.listFiles( ). Each FTPFile object contains information describing the remote file, and the name and size of each FTPFile is printed to the console as follows:

Files in /pub/mirrors/apache/jakarta/ecs/binaries
README.html:           1 KB
RELEASE_NOTES.txt:     2 KB
ecs-1.4.2.tar.gz:      1 MB
ecs-1.4.2.tar.gz.asc:  65 bytes
ecs-1.4.2.tar.gz.md5:  33 bytes
ecs-1.4.2.zip:         2 MB
ecs-1.4.2.zip.asc:     65 bytes
ecs-1.4.2.zip.md5:     33 bytes

See Also

Commons Net also contains a Trivial File Transfer Protocol (TFTP) client: org.apache.commons.net.tftp.TFTPClient. For more information about TFTP client, see the Javadoc for TFTPClient at http://jakarta.apache.org/commons/net/apidocs/org/apache/commons/net/tftp/TFTPClient.html.

FTPClient contains a number of additional features, such as active and passive connection modes and the ability to append to remote files, make remote directories, and put files on a remote FTP server. For more information about FTPClient, see the FTPClient Javadoc at http://jakarta.apache.org/commons/net/apidocs/org/apache/commons/net/ftp/FTPClient.html.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.98.148