| DirList.java |
1 /*
2 * Copyright 2005 jMethods, Inc. All rights reserved.
3 * jMethods PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
4 */
5
6 package com.myjavaworld.ftp.examples;
7
8 import java.text.DateFormat;
9 import java.text.ParseException;
10 import java.util.Date;
11
12 import com.myjavaworld.ftp.ConnectionException;
13 import com.myjavaworld.ftp.ControlConnectionEvent;
14 import com.myjavaworld.ftp.ControlConnectionListener;
15 import com.myjavaworld.ftp.FTPClient;
16 import com.myjavaworld.ftp.FTPConnectionEvent;
17 import com.myjavaworld.ftp.FTPConnectionListener;
18 import com.myjavaworld.ftp.FTPException;
19 import com.myjavaworld.ftp.ListParser;
20 import com.myjavaworld.ftp.RemoteFile;
21
22 /**
23 * This example demonstrates how to connect to an FTP site and list the contents
24 * of a directory.
25 *
26 * @author Sai Pullabhotla, psai [at] jMethods [dot] com
27 * @version 1.0
28 */
29 public class DirList {
30
31 /**
32 * A date format object format last modified date of remote files.
33 */
34 private static final DateFormat dateFormat =
35 DateFormat.getDateTimeInstance();
36
37 /**
38 * @param args
39 * command line arguments.
40 * @throws InstantiationException
41 * propogated
42 * @throws ClassNotFoundException
43 * propogated
44 * @throws IllegalAccessException
45 * propogated
46 * @throws FTPException
47 * propogated
48 * @throws ConnectionException
49 * propogated
50 * @throws ParseException
51 * propogated
52 */
53
54 public static void main(String[] args) throws InstantiationException,
55 ClassNotFoundException, IllegalAccessException, FTPException,
56 ParseException, ConnectionException {
57
58 //Determine the FTPClient implementation that you would like to use.
59 //For most FTP servers, the DefaultFTPClient should work. There is
60 //another implementation for working with AS/400 (iSeries) FTP servers.
61 //The class name for it is com.myjavaworld.ftp.AS400FTPClient.
62 //And, of course, you can always write your own implementation either
63 //by implementing the FTPClient interface or by extending one of the
64 //exsting implementations.
65
66 String ftpClientClassName = "com.myjavaworld.ftp.DefaultFTPClient";
67 //String ftpClientClassName = "com.myjavaworld.ftp.AS400FTPClient";
68
69 //Determine the directory list parser that we would like to use in this
70 //session. For most FTP servers, the default implementatiom should work
71 //fine. There is another implementation provided for handling
72 //MS-DOS directory list format. The class name is
73 //com.myjavaworld.ftp.DosListPareser. You can always write one of your
74 //own implementation if you want to work with an FTP server that uses a
75 //list format other than UNIX or DOS. This can be done implementing the
76 //com.myjavaworld.ftp.ListParser interface.
77
78 String listParserClassName = "com.myjavaworld.ftp.DefaultListParser";
79 //String listParserClassName = "com.myjavaworld.ftp.DosListParser";
80
81 //Use reflection to find the FTP Client class and obtain an instance
82 //of FTPClient.
83 FTPClient client = (FTPClient) Class.forName(ftpClientClassName)
84 .newInstance();
85
86 //Use reflection to find the list parser class and obtain an instance
87 //of the list parser.
88 ListParser parser = (ListParser) Class.forName(listParserClassName)
89 .newInstance();
90
91 //Set the list parser to use with this FTP client.
92 client.setListParser(parser);
93
94 //Listen for connection events. This is optional.
95 client.addFTPConnectionListener(new FTPConnectionListener() {
96 public void connectionOpened(FTPConnectionEvent evt) {
97 System.out.println(evt.getMessage());
98 }
99
100 public void connectionClosed(FTPConnectionEvent evt) {
101 System.out.println(evt.getMessage());
102 }
103 });
104
105 //Listen for various commands that are sent to the server and the
106 //replies received. This is optional.
107 client.addControlConnectionListener(new ControlConnectionListener() {
108 public void commandSent(ControlConnectionEvent evt) {
109 System.out.println(evt.getMessage());
110 }
111
112 public void replyReceived(ControlConnectionEvent evt) {
113 System.out.println(evt.getMessage());
114 }
115 });
116
117 //Connect to the FTP server.
118 client.connect("ftp.netscape.com");
119
120 //Login to the FTP server.
121 client.login("anonymous", "you@yourcompany.com");
122
123 //Findout the current working directory on the FTP server.
124 RemoteFile workingDirectory = client.getWorkingDirectory();
125
126 //print the dir listing.
127 list(client);
128
129 //Change the working directory to a different directory. Make sure that
130 //this dir exists on the FTP server.
131 client.setWorkingDirectory(parser.createRemoteFile(workingDirectory,
132 "usr", true));
133
134 //Print the dir list of the new working directory.
135 list(client);
136
137 //Close the connection.
138 client.disconnect();
139 }
140
141 private static void list(FTPClient client) throws FTPException,
142 ParseException, ConnectionException {
143 RemoteFile[] children = client.list();
144 //For each file in the list, print out the attributes, whether the file
145 // is a directory or a regular file, the size of the file, the date and
146 // the name of the file.
147 for(int i = 0; i < children.length; i++) {
148 System.out.println(children[i].getAttributes() + "\t"
149 + (children[i].isDirectory() ? "<DIR>" : "<FILE>") + "\t"
150 + children[i].getSize() + "\t"
151 + dateFormat.format(new Date(children[i].getLastModified()))
152 + "\t" + children[i].getName());
153 }
154 }
155 }