【问题标题】:FTP populate jtree with files & directories from serverFTP 使用来自服务器的文件和目录填充 jtree
【发布时间】:2013-09-15 11:22:37
【问题描述】:

我正在使用 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));

【问题讨论】:

    标签: java swing apache ftp jtree


    【解决方案1】:

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

    【讨论】:

    • 我知道如何在我的本地 PC 上执行此操作,但是通过 Apache commons 从远程服务器获取文件并将它们放在 jtree 中是我正在寻找的......。任何帮助
    • 对 Apache 库的调用将进入 worker 的 doInBackground() 方法;我会说 publish() 每个 FTPFile 由引擎的 getNext() 返回。
    • 有一个代码来做这就是我需要的,这是我对 java 的新手并且真的很挣扎......我正在自学所有这些并且我被卡住了......我可以请一个代码示例
    • 我不知道你卡在哪里了。整个程序可能有几百行;请编辑您的问题以包含sscce,该sscce 专注于您遇到的问题。
    猜你喜欢
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多