#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    7
    Rep Power
    0

    Cool FTP populate jtree with files & directories from server


    Am using Apache commons and am trying to show specified files and directories from a server,does anyone know how to do this,i cant find anyone who does on the internet...neither am i getting a sample code......someone please help .Thank you n advance
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    7
    Rep Power
    0
    this is the code i found so far and it seems not to work, can some please help me or am i alone on this and have to figure myself out

    Code:
    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));
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    7
    Rep Power
    0
    Originally Posted by nascal3
    this is the code i found so far and it seems not to work, can some please help me or am i alone on this and have to figure myself out

    Code:
    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));
    got this code from http://forums.devshed.com/java-help-9/jtree-ftp-file-browser-894260.html
  6. #4
  7. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,021
    Rep Power
    1285
    Are you required to use Apache Commons? Have you tried Jsch?
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    7
    Rep Power
    0
    Originally Posted by bullet
    Are you required to use Apache Commons? Have you tried Jsch?
    No i haven't is it better?, have not even heard of it
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    7
    Rep Power
    0
    Originally Posted by bullet
    Are you required to use Apache Commons? Have you tried Jsch?
    can you show me how to do that using Jsch but most preferably Apache commons if also possible
  12. #7
  13. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,021
    Rep Power
    1285
    What is happening when you run the code? Are you sure the server allows FTP?
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    7
    Rep Power
    0
    Originally Posted by bullet
    What is happening when you run the code? Are you sure the server allows FTP?
    The jtree is not generated but it rather just keeps requesting the file list from the FTP sever continuously in a loop and the loop does not stop till you cancel the program when you force it to logout of the FTP server in gives error of "pointer null"

IMN logo majestic logo threadwatch logo seochat tools logo