簡體   English   中英

Java-遠程文件瀏覽器

[英]Java - Remote file browser

我進行了一些搜索,但未找到任何答案。

我計划為用Java編碼的服務器制作一個完整的遠程工具。 我已經成功完成了將文件發送到服務器的任務,但是現在我想使用文件瀏覽器列出並獲取所有文件。 我知道如何制作本地jFileChooser,但可以將其設為遠程嗎?

我用插座連接到服務器。

謝謝。

實際上已經有人做出了您要嘗試的內容,並將其托管在sourceforge上。 您也可以獲取源代碼。 檢查vfsjfilechooser

要比較vjsfilechooser方法和JFileChooser API,可以閱讀下面提到的url。 http://www.loni.ucla.edu/twiki/bin/view/CCB/VFSBrowserProgrammersGuide?skin=plain&sortcol=1&table=1&up=1

我不認為您可以將JFileChooser配置為遠程,但您應該能夠根據其代碼編寫自己的代碼,甚至可以將其子類化。

如果您要編寫類似Windows文件選擇器的文件,通常認為它更好。

您可以基於VFS或類似的文件,使其可以與任何文件系統一起使用。

假設它是一個基於Web的應用程序,您想從應用程序服務器中選擇一個文件。 檢查這是否真的是一個不錯的選擇,因為我可以清楚地了解服務器文件結構。 肯定會有安全威脅。

package learnings;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;

public class SimpleLinuxGUI {

String sshremotedir = "GiveRemoteDirectoryPath";
public static void cargarRTree(String remotePath, DefaultMutableTreeNode parent) throws SftpException, JSchException { 
//todo: change "/" por remote file.separator
JSch jsch = new JSch();
Session session = null;
session = jsch.getSession(username, hostname, 22);
session.setConfig("StrictHostKeyChecking", "no");
session.setPassword(password);
session.connect();
Channel channel = session.openChannel("sftp");
channel.connect();
ChannelSftp sftpChannel = (ChannelSftp) channel;
Vector<ChannelSftp.LsEntry> list = sftpChannel.ls(remotePath); // List source directory structure.
    System.out.println(list);
//Vector<ChannelSftp.LsEntry> list = sftpChannel
for (ChannelSftp.LsEntry oListItem : list) { // Iterate objects in the list to get file/folder names.       
    DefaultMutableTreeNode node = new DefaultMutableTreeNode(oListItem.getFilename());
    if (!oListItem.getAttrs().isDir()) { // If it is a file (not a directory).
        parent.add(node); // add as a child node
    } else{
        if (!".".equals(oListItem.getFilename()) && !"..".equals(oListItem.getFilename())) {
            parent.add(node); // add as a child node
            cargarRTree(remotePath + "/" + oListItem.getFilename(), node); // call again for the subdirectory
        }
    }
}

}

public static void main(String[] args) {
    SimpleLinuxGUI slg = new SimpleLinuxGUI();
    JFrame jf = new JFrame();


    DefaultMutableTreeNode nroot = new DefaultMutableTreeNode(slg.sshremotedir);                
    try {
        cargarRTree(slg.sshremotedir, nroot);
    } catch (SftpException e1) {
// TODO Auto-generated catch block
        e1.printStackTrace();
    }       catch (JSchException ex) { 
        Logger.getLogger(SimpleLinuxGUI.class.getName()).log(Level.SEVERE, null, ex);
    } 
    JTree yourJTree = new JTree(nroot);
    jf.add(yourJTree);
    jf.setSize(640, 480);
    jf.setVisible(true);
}

}

該代碼將幫助您獲取遠程服務器中的文件和目錄列表,然后在GUI中創建Jtree和Display。現在,您可以通過添加操作偵聽器和添加按鈕來滿足要求來更改GUI。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM