我在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);
}
}
}
现在,我使用此代码打开资源管理器,在单击JButton时选择本地计算机(Windows)中的目录:
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new java.io.File(""));
chooser.setDialogTitle("choosertitle");
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
chooser.setAcceptAllFileFilterUsed(false);
if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
System.out.println("getCurrentDirectory(): " + chooser.getCurrentDirectory());
selectLabel.setText(chooser.getSelectedFile().toString());
System.out.println("getSelectedFile() : " + chooser.getSelectedFile());
} else {
System.out.println("No Selection ");
}
从那时起,我认为我必须修改最后一个代码的第二行替换&#34; java.io.File(&#34;&#34;)&#34;由于SSH连接,通过Unix机器的路径。
因此,一旦我获得了SSH连接的路径(例如使用&#34; pwd&#34;命令),我如何调整我的第二个代码以在Unix机器上打开资源管理器而不是在本地机器?
感谢您的时间,如果我没有提供足够的信息,请随时问我。
答案 0 :(得分:1)
这将创建一个临时目录,复制临时目录中〜/ Documents文件夹的文件结构,然后在temp目录中显示文件选择器。这非常粗糙,但如果您通过SSH运行find
命令,它应该执行您所询问的内容。
如果您想要更强大的解决方案,请考虑使用您自己的实现或使用Guava中包含的工具创建FileSystem
。
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();
}
}
}
以下是我提出的要点:https://gist.github.com/prestongarno/b0034ae37ca5e757a35f996b4c72620d