简体   繁体   中英

Java - Remote file browser

I've made some searches but I've found nothing about that.

I plan to make a full remote tool for my server coded in java. I have successfully made the part to send a file to the server but now I want to use a file browser to list and get all files. I know how to make a local jFileChooser but is it possible to make it remote?

I connect to my server with a socket.

Thanks.

Someone actually already made what you are trying and hosted that on sourceforge. You can get the source code too. Check vfsjfilechooser

To get a comparison of vjsfilechooser methods and JFileChooser API you can read through the url mentioned below. http://www.loni.ucla.edu/twiki/bin/view/CCB/VFSBrowserProgrammersGuide?skin=plain&sortcol=1&table=1&up=1

I don't imagine you can configure JFileChooser to be remote but you should be able to write your own based on its code or even sub-class it.

If you are going to write a look-a-like the Windows file chooser is generally considered to be nicer.

You could base it on VFS or similar so it can work with any filesystem.

Assuming its a web based application , you want to select a file from the application server. Check if that is really a good option , because i can get a clear view and idea of the server file structure. There would be definitely a security threat.

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

}

This Code will help you to Get the list of files and directories in Remote server then creates a Jtree and Display in GUI.And now You can make changes to GUI by adding action listeners and adding buttons to achieve your requirements

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM