Index: /home/im/projects/jakarta-commons/net/src/java/examples/ftp.java =================================================================== --- /home/im/projects/jakarta-commons/net/src/java/examples/ftp.java (revision 331835) +++ /home/im/projects/jakarta-commons/net/src/java/examples/ftp.java (working copy) @@ -24,6 +24,7 @@ import org.apache.commons.net.ftp.FTP; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPConnectionClosedException; +import org.apache.commons.net.ftp.FTPFile; import org.apache.commons.net.ftp.FTPReply; /*** @@ -40,10 +41,11 @@ { public static final String USAGE = - "Usage: ftp [-s] [-b] \n" + + "Usage: ftp [-s] [-b] [-l[a]] \n" + "\nDefault behavior is to download a file and use ASCII transfer mode.\n" + "\t-s store file on server (upload)\n" + - "\t-b use binary transfer mode\n"; + "\t-b use binary transfer mode\n" + + "\t-l[a] list [hidden] files (no not allowed)\n"; public static final void main(String[] args) { @@ -48,7 +50,7 @@ public static final void main(String[] args) { int base = 0; - boolean storeFile = false, binaryTransfer = false, error = false; + boolean storeFile = false, binaryTransfer = false, error = false, listFiles = false, listHidden = false; String server, username, password, remote, local; FTPClient ftp; @@ -58,6 +60,13 @@ storeFile = true; else if (args[base].startsWith("-b")) binaryTransfer = true; + else if (args[base].startsWith("-la")) + { + listHidden = true; + listFiles = true; + } + else if (args[base].startsWith("-l")) + listFiles = true; else break; } @@ -62,7 +71,7 @@ break; } - if ((args.length - base) != 5) + if ((!listFiles && (args.length - base) != 5) || (listFiles && (args.length - base) < 4)) { System.err.println(USAGE); System.exit(1); @@ -72,9 +81,17 @@ username = args[base++]; password = args[base++]; remote = args[base++]; - local = args[base]; + if (base < args.length) + { + local = args[base]; + } + else + { + local = null; + } ftp = new FTPClient(); + ftp.setListHiddenFiles(listHidden); ftp.addProtocolCommandListener(new PrintCommandListener( new PrintWriter(System.out))); @@ -132,7 +149,24 @@ // behind firewalls these days. ftp.enterLocalPassiveMode(); - if (storeFile) + if (listFiles) + { + FTPFile[] files = ftp.listFiles(remote); + if (files != null) + { + for (int i = 0; i * @return The status information returned by the server. @@ -2485,7 +2501,7 @@ Socket socket; FTPFile[] results; - if ((socket = _openDataConnection_(FTPCommand.LIST, pathname)) == null) + if ((socket = _openDataConnection_(FTPCommand.LIST, getListArguments(pathname))) == null) return new FTPFile[0]; results = parser.parseFileList(socket.getInputStream(), getControlEncoding()); @@ -2625,7 +2641,7 @@ { Socket socket; - if ((socket = _openDataConnection_(FTPCommand.LIST, pathname)) == null) + if ((socket = _openDataConnection_(FTPCommand.LIST, getListArguments(pathname))) == null) { return null; } @@ -2667,7 +2683,26 @@ public void configure(FTPClientConfig config) { this.__configuration = config; } - + + /** + * You can set this to true if you would like to get hidden files when {@link #listFiles} too. + * A LIST -a will be issued to the ftp server. + * It depends on your ftp server if you need to call this method, also dont expect to get rid + * of hidden files if you call this method with "false". + * + * @param listHiddenFiles true if hidden files should be listed + */ + public void setListHiddenFiles(boolean listHiddenFiles) { + this.__listHiddenFiles = listHiddenFiles; + } + + /** + * @see #setListHiddenFiles(boolean) + * @return the current state + */ + public boolean getListHiddenFiles() { + return this.__listHiddenFiles; + } } /* Emacs configuration