Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何打开资源管理器以选择具有SSH连接的目录?_Java_Linux_Unix_Ssh - Fatal编程技术网

Java 如何打开资源管理器以选择具有SSH连接的目录?

Java 如何打开资源管理器以选择具有SSH连接的目录?,java,linux,unix,ssh,Java,Linux,Unix,Ssh,我有一个在Windows上运行的Java程序。我需要用户选择Unix计算机中的目录,而不是本地计算机中的目录 我有一个到这个Unix机器的SSH连接,多亏了BufferedReader,我可以得到一些命令的结果,比如“pwd”。代码如下: import com.jcraft.jsch.*; import sshtest.Exec.MyUserInfo; import java.io.*; public class SSHConnection { public static void

我有一个在Windows上运行的Java程序。我需要用户选择Unix计算机中的目录,而不是本地计算机中的目录

我有一个到这个Unix机器的SSH连接,多亏了BufferedReader,我可以得到一些命令的结果,比如“pwd”。代码如下:

import com.jcraft.jsch.*;
import sshtest.Exec.MyUserInfo;
import java.io.*;

public class SSHConnection {

    public static void main(String[] args) {
        try {
            JSch jsch = new JSch();
            String user = "myUserId";
            String host = "unixmachines.company.corp";
            Session session = jsch.getSession(user, host, 22);

            UserInfo ui = new MyUserInfo();
            session.setUserInfo(ui);
            session.connect();

            String command = "pwd";

            Channel channel = session.openChannel("exec");
            InputStream in = channel.getInputStream();

            ((ChannelExec)channel).setCommand(command);         
            ((ChannelExec)channel).setErrStream(System.err);

            channel.connect();

            BufferedReader reader = new BufferedReader(new InputStreamReader(in));
            String line;
            int index = 0;

            while ((line = reader.readLine()) != null)
            {
                System.out.println(++index + " : " + line);
            }

            byte[] tmp=new byte[1024];
            while(true){
                while(in.available()>0){
                    int i=in.read(tmp, 0, 1024);
                    if(i<0)break;
                    System.out.print(new String(tmp, 0, i));
                }
                if(channel.isClosed()){
                    if(in.available()>0) continue;
                    System.out.println("exit-status: "+channel.getExitStatus());
                    break;
                }
                try{
                    Thread.sleep(1000);
                }
                catch(Exception ee){}
            }
            channel.disconnect();
            session.disconnect();
        }
        catch(Exception e){
              System.out.println(e);
        }
    }

}
从那时起,我认为我必须修改最后一段代码的第二行,将“java.io.File(“”)替换为Unix机器的路径,这要感谢SSH连接

因此,一旦我获得了SSH连接的路径(例如使用“pwd”命令),我如何调整我的第二个代码以在Unix机器上而不是在本地机器上打开资源管理器


感谢您的时间,如果我没有提供足够的信息,请随时询问我。

这将创建一个临时目录,将我的~/Documents文件夹的文件结构复制到临时目录中,然后在临时目录中显示文件选择器。这确实很粗糙,但是如果您通过SSH运行
find
命令,它应该满足您的要求

如果你想要一个更健壮的解决方案,考虑使用你自己的实现创建一个<代码>文件系统< /代码>,或者使用一个类似于番石榴中包含的工具。

import java.io.*;
import java.nio.file.*;
import java.nio.file.spi.FileSystemProvider;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import javax.swing.*;

public class CustomFileChooser {

    public static void main(String[] args) throws IOException {
        Function<String, Integer> countDir = str -> (str.length() - str.replace("/", "").length());

        List<String> directories = runCommand("cd ~/Documents && find . -type d ")
                .stream()
                .map(str -> str.substring(1))
                .sorted((o1, o2) -> {
                    Integer o1Count = countDir.apply(o1);
                    Integer o2count = countDir.apply(o2);
                    if (o1Count > o2count) return 1;
                    else if (o2count > o1Count) return -1;
                    else return 0;
                })
                .collect(Collectors.toList());

        String url = System.getProperty("java.io.tmpdir") + "/javaFileChooser";
        File file = new File(url);
        Path p = Files.createDirectory(file.toPath());

        Runtime.getRuntime().addShutdownHook(new Thread(() -> runCommand("rm -rf " + file.toPath())));

        FileSystemProvider.installedProviders().get(0).checkAccess(file.toPath(), AccessMode.WRITE);

        directories.forEach(str -> {
            try {
                Files.createDirectory(new File(file.toPath().toString() + "/" + str).toPath());
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        final JFileChooser fc = new JFileChooser();
        fc.setCurrentDirectory(file);
        fc.setSize(400, 400);
        fc.setVisible(true);
        fc.setControlButtonsAreShown(true);
        int returnVal = fc.showOpenDialog(null);

    }

    public static List<String> runCommand(String command) {
        try (InputStream inputStream = Runtime.getRuntime()
                .exec(new String[]{"sh", "-c", command}).getInputStream();
             Reader in = new InputStreamReader(inputStream)) {

            return new BufferedReader(in).lines().collect(Collectors.toList());

        } catch (IOException e) {
            return Collections.emptyList();
        }
    }
}
import java.io.*;
导入java.nio.file.*;
导入java.nio.file.spi.FileSystemProvider;
导入java.util.*;
导入java.util.function.function;
导入java.util.stream.collector;
导入javax.swing.*;
公共类CustomFileChooser{
公共静态void main(字符串[]args)引发IOException{
函数countDir=str->(str.length()-str.replace(“/”,“”)。length());
List directories=runCommand(“cd~/Documents&&find.-type d”)
.stream()
.map(str->str.substring(1))
.已排序((o1,o2)->{
整数o1Count=countDir.apply(o1);
整数o2count=countDir.apply(o2);
如果(O1计数>O2计数)返回1;
否则如果(o2count>o1Count)返回-1;
否则返回0;
})
.collect(Collectors.toList());
字符串url=System.getProperty(“java.io.tmpdir”)+“/javaFileChooser”;
文件=新文件(url);
路径p=Files.createDirectory(file.toPath());
Runtime.getRuntime().addShutdownHook(新线程(()->runCommand(“rm-rf”+file.toPath()));
FileSystemProvider.installedProviders().get(0).checkAccess(file.toPath(),AccessMode.WRITE);
目录。forEach(str->{
试一试{
Files.createDirectory(新文件(File.toPath().toString()+“/”+str.toPath());
}捕获(例外e){
e、 printStackTrace();
}
});
final JFileChooser fc=新的JFileChooser();
fc.setCurrentDirectory(文件);
fc.设置大小(400400);
fc.setVisible(真);
fc.setControlButtonsAreShown(真);
int returnVal=fc.showOpenDialog(null);
}
公共静态列表运行命令(字符串命令){
try(InputStream InputStream=Runtime.getRuntime()
.exec(新字符串[]{“sh”,“-c”,command}).getInputStream();
Reader in=新的InputStreamReader(inputStream)){
返回新的BufferedReader(in).lines().collect(Collectors.toList());
}捕获(IOE异常){
返回集合。emptyList();
}
}
}
以下是我的要点:

import java.io.*;
import java.nio.file.*;
import java.nio.file.spi.FileSystemProvider;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import javax.swing.*;

public class CustomFileChooser {

    public static void main(String[] args) throws IOException {
        Function<String, Integer> countDir = str -> (str.length() - str.replace("/", "").length());

        List<String> directories = runCommand("cd ~/Documents && find . -type d ")
                .stream()
                .map(str -> str.substring(1))
                .sorted((o1, o2) -> {
                    Integer o1Count = countDir.apply(o1);
                    Integer o2count = countDir.apply(o2);
                    if (o1Count > o2count) return 1;
                    else if (o2count > o1Count) return -1;
                    else return 0;
                })
                .collect(Collectors.toList());

        String url = System.getProperty("java.io.tmpdir") + "/javaFileChooser";
        File file = new File(url);
        Path p = Files.createDirectory(file.toPath());

        Runtime.getRuntime().addShutdownHook(new Thread(() -> runCommand("rm -rf " + file.toPath())));

        FileSystemProvider.installedProviders().get(0).checkAccess(file.toPath(), AccessMode.WRITE);

        directories.forEach(str -> {
            try {
                Files.createDirectory(new File(file.toPath().toString() + "/" + str).toPath());
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        final JFileChooser fc = new JFileChooser();
        fc.setCurrentDirectory(file);
        fc.setSize(400, 400);
        fc.setVisible(true);
        fc.setControlButtonsAreShown(true);
        int returnVal = fc.showOpenDialog(null);

    }

    public static List<String> runCommand(String command) {
        try (InputStream inputStream = Runtime.getRuntime()
                .exec(new String[]{"sh", "-c", command}).getInputStream();
             Reader in = new InputStreamReader(inputStream)) {

            return new BufferedReader(in).lines().collect(Collectors.toList());

        } catch (IOException e) {
            return Collections.emptyList();
        }
    }
}