0

Am using Apache commons and am trying to show specified files and directories from a server,does anyone know how to do this,av used this code so far but its really not working. Can someone help out or show me where am going wrong.

 public String[] getDir(String rootDirectory) {
    String server = "192.168.1.11";
    int port = 21;
    String user = "javaapp";
    String pass = "nascalebio";
    String Directory = "/cms";
    String[] directories;

    FTPClient ftpClient = new FTPClient();

    try {
        ftpClient.connect(server, port);
        ftpClient.login(user, pass);

        FTPFile[] files = ftpClient.mlistDir(Directory);
        directories = new String[files.length];
        for (int i =0; i < files.length; i++) {
            directories[i] = files[i].getName();
            System.out.println(i);
            System.out.println(directories[i]);
            System.out.println(files.length);
           }

        return directories;

    } catch (IOException e) {
        System.out.println(e);
    }
    return null;

}

public void buildtree(String currentdir, DefaultMutableTreeNode model) throws SocketException, IOException {
    String[] currentcrawl = getDir(currentdir);
    for (String node : currentcrawl) {
    DefaultMutableTreeNode currentnode = new DefaultMutableTreeNode(node);
    buildtree(currentdir +"/" + node, currentnode);
    model.add(currentnode);

    }



    buildtree(".", root);
    tree.setModel(new DefaultTreeModel(root));
trashgod
  • 203,806
  • 29
  • 246
  • 1,045

1 Answers1

2

Consider the several related examples examined here. Basically you're going to need to build a TreeModel, such as FileTreeModel, and use it in a view, such as JTree or Outline. Because of network latency, you'll need to fetch files in the background, perhaps using SwingWorker, and update your TreeModel in your implementation of the worker's publish() method.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • I know how to do that on my local P.C but to fetch files from a remote server via Apache commons and place them in a jtree is what am looking for.......any help – Caleb Nasio Sep 20 '13 at 15:13
  • The calls to the Apache library would go in the worker's `doInBackground()` method; I'd say `publish()` each `FTPFile` returned by the engine's `getNext()`. – trashgod Sep 20 '13 at 16:12
  • Having a code that does that is what i need am new to java an d really struggling......am teaching myself all these and am stuck.....can i please get an example of the code – Caleb Nasio Sep 24 '13 at 18:32
  • I don't know where you're stuck. The entire program will likely be several hundred lines; please edit your question to include an [sscce](http://sscce.org/) that focuses on the problem you're having. – trashgod Sep 24 '13 at 20:01