Dave Kennedy wrote:
I need to write an FTP client to upload/download files from a server
I've implemented StrutsFileUplaod and StrutsFileDownload from the Struts
Wiki
Then you aren't using FTP, right? Your using HTTP for uploads and
downloads :)
How can I retrieve and display a directory/file list of the server?
Does anyone have a Struts example which displays a list of directories and
files?
There's nothing in Struts that will do this out of the box, you'll have
to write code to do it. A few years ago, as my first project to learn
Struts, I wrote a rudimentary file manager application... I just dug it
out of the archives, and while I can see a number of things I wouldn't
be doing today (live and learn!), it should at least give you a good
idea how to do what you want...
package com.omnytex.ofm.actions;
import java.io.*;
import java.util.*;
import java.text.*;
import javax.servlet.http.*;
import org.apache.struts.*;
import org.apache.struts.action.*;
import com.omnytex.ofm.actionforms.*;
import com.omnytex.ofm.config.*;
public class ListPathContentsAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {
// Cheapo logging
System.out.println(this.getClass().getName());
// Get reference to the correct ActionForm
ListPathContentsActionForm lpcaf = (ListPathContentsActionForm)form;
if (lpcaf == null) {
lpcaf = new ListPathContentsActionForm();
}
// Get the path passed in from the view
String path = lpcaf.getPath();
// If the last character of path is NOT backslash, it means the path is
// NOT a root drive. Therefore, we need to add that character so that
// paths will be constructed properly as we walk the directory
hierarchy.
// Remember that path can be null the first time through or when
// a drive is selected!
if (path != null && !path.endsWith("\\")) {
path += "\\";
}
// First, get a list of all files and directories in the path we
were passed
// from the view, if any. Remember that path can be null the first
time
// through or when a drive is selected!
String[] sContents = null;
if (path != null) {
sContents = (new File(lpcaf.getPath())).list();
}
// Now construct an ArrayList from the String[] array above. For each
// item we need to determine if it's a file or directory and create
// a HashMap for each, which is what gets added to our ArrayList.
// Remember that sContents will still be null if this is the first time
// through, or if a drive was selected.
ArrayList alContents = new ArrayList();
if (sContents != null) {
for (int i = 0; i < sContents.length; i++) {
String qualifiedName = path + sContents[i];
HashMap hm = new HashMap();
hm.put("name", sContents[i]);
hm.put("qualifiedName", qualifiedName);
File f = new File(qualifiedName);
hm.put("isDir", new Boolean(f.isDirectory()));
hm.put("length", new DecimalFormat().format(f.length()));
Date lm = new Date(f.lastModified());
hm.put("lastModified", new
SimpleDateFormat("MM/dd/yyyy").format(lm));
alContents.add(hm);
}
}
// "Sort", sort of, the array so directories appear first
ArrayList dList = new ArrayList();
ArrayList fList = new ArrayList();
for (Iterator it = alContents.iterator(); it.hasNext();) {
HashMap h = (HashMap)it.next();
if (((Boolean)h.get("isDir")).booleanValue()) {
dList.add(h);
} else {
fList.add(h);
}
}
alContents.clear();
for (Iterator it = dList.iterator(); it.hasNext();) {
alContents.add(it.next());
}
for (Iterator it = fList.iterator(); it.hasNext();) {
alContents.add(it.next());
}
// Put the ArrayList in the ActionForm
lpcaf.setContents(alContents);
// Get a list of drives and put it in the ActionForm
File[] drives = File.listRoots();
lpcaf.setDrives(drives);
// Go to the correct view
return mapping.findForward("showPathContents");
} // End execute()
} // End class
FYI, the ActionForm was pretty trivial, just had the following fields
and corresponding setters/getters:
ArrayList contents
String path
ArrayList drives
hth,
Frank
--
Frank W. Zammetti
Founder and Chief Software Architect
Omnytex Technologies
http://www.omnytex.com
AIM/Yahoo: fzammetti
MSN: [EMAIL PROTECTED]
Author of "Practical Ajax Projects With Java Technology"
(2006, Apress, ISBN 1-59059-695-1)
Java Web Parts - http://javawebparts.sourceforge.net
Supplying the wheel, so you don't have to reinvent it!
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]