FTP用文件填充jtree&服务器的目录

时间:2013-09-15 11:22:38

标签: java swing apache ftp jtree

我正在使用Apache commons并且我试图从服务器显示指定的文件和目录,有没有人知道如何做到这一点,到目前为止av使用了这个代码,但它真的不起作用。有人可以帮忙或告诉我哪里出错了。

 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));

1 个答案:

答案 0 :(得分:2)

考虑一下所研究的几个相关例子here。基本上,您需要构建TreeModel,例如FileTreeModel,并在视图中使用它,例如JTreeOutline。由于网络延迟,您需要在后台获取文件,可能使用SwingWorker,并在工作人员TreeModel方法的实施中更新publish()