[英]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.