我有一個在Windows上運行的Java程序。我需要用戶在Unix機器上選擇一個目錄,而不是在本地機器上。如何打開資源管理器以通過SSH連接選擇目錄?
我有一個SSH連接到這臺Unix機器,並感謝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);
}
}
}
現在,我使用此代碼打開資源管理器,選擇在本地機器上的一個目錄(Windows)在一個JButton點擊時:
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 ");
}
從那時起,我認爲我必須修改最後一個代碼的第二行,用Unix連接的路徑替換「java.io.File(」「)」,這要歸功於SSH連接。因此,一旦我通過SSH連接(例如「pwd」命令)獲得路徑,我該如何調整我的第二個代碼以在Unix機器上而不是在本地機器上打開一個資源管理器?
謝謝你的時間,不要猶豫,問我是否沒有提供足夠的信息。