2017-08-21 55 views
1

我有一個在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機器上而不是在本地機器上打開一個資源管理器?

謝謝你的時間,不要猶豫,問我是否沒有提供足夠的信息。

回答

1

這將創建一個臨時目錄,將我的〜/ Documents文件夾的文件結構複製到臨時目錄中,然後將文件選擇器顯示在臨時目錄中。這非常粗糙,但是它應該執行你詢問的有關如果通過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

相關問題