« Sending Email Using Java | Main | Secure FTP Using Java and FTPS (FTP over SSL) »

March 10, 2008

FTP Directory Listing Using Java

Overview

The ability to transfer files from one machine to another is often accomplished using an FTP client. Using the Java FTP component provided in Secure FTP Factory this article will demonstrate how you can easily embed FTP capabilities into your own Java applications.

Code Example

Download FtpExample.java (2.3K)

01 /*
02  * @(#)FtpExample.java
03  *
04  * Copyright (c) JSCAPE LLC.
05  *
06  * This software is the confidential and proprietary information of
07  * JSCAPE. ("Confidential Information").  You shall not disclose such
08  * Confidential Information and shall use it only in accordance with
09  * the terms of the license agreement you entered into with JSCAPE.
10  */
11
12 import com.jscape.inet.ftp.*;
13 import java.io.*;
14 import java.util.Enumeration;
15
16 public class FtpExample extends FtpAdapter {
17  private String hostname;
18  private String username;
19  private String password;
20  
21  public FtpExample(String hostname, String username, String password) {
22  this.hostname = hostname;
23  this.username = username;
24  this.password = password;
25  }
26  
27  // print out directory listing
28  public void getListing() throws FtpException {
29  Ftp ftp = new Ftp(hostname,username,password);
30  
31  //capture Ftp related events
32  ftp.addFtpListener(this);
33  ftp.connect();
34  
35  String results = ftp.getDirListingAsString();
36  System.out.println(results);
37  ftp.disconnect();
38  }
39  
40  // captures connect event
41  public void connected(FtpConnectedEvent evt) {
42  System.out.println("Connected to server: " + evt.getHostname());
43  }
44  
45  // captures disconnect event
46  public void disconnected(FtpDisconnectedEvent evt) {
47  System.out.println("Disconnected from server: " + evt.getHostname());
48  }
49  
50  
51  public static void main(String[] args) {
52  String hostname;
53  String username;
54  String password;
55  try {
56  BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
57  System.out.print("Enter Ftp hostname (e.g. ftp.yourhost.com): ");
58  hostname = reader.readLine();
59  System.out.print("Enter username (e.g. anonymous): ");
60  username = reader.readLine();
61  System.out.print("Enter password (e.g. user@here.com): ");
62  password = reader.readLine();
63  FtpExample example = new FtpExample(hostname,username,password);
64  example.getListing();
65  }
66  catch(Exception e) {
67  e.printStackTrace();
68  }
69  }
70 }
 

  1. Lines 12-14.  Add necessary import statements.
  2. Lines 29-33.  Create new Ftp instance, add listener and connect.
  3. Lines 35-37.  Get directory listing, print results and disconnect.
  4. Lines 41-48.  Event handling methods.

TrackBack

TrackBack URL for this entry:
http://www.typepad.com/t/trackback/2925084/27108042

Listed below are links to weblogs that reference FTP Directory Listing Using Java:

Comments

Feed You can follow this conversation by subscribing to the comment feed for this post.

Post a comment

Comments are moderated, and will not appear on this weblog until the author has approved them.

If you have a TypeKey or TypePad account, please Sign In