2017-08-21 7 views
1

私はWindows上でJavaプログラムを実行しています。私は、ローカルマシンではなくUnixマシンでディレクトリを選択する必要があります。エクスプローラを開いてSSH接続のあるディレクトリを選択する方法は?

私はこの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); 
     } 
    } 

} 

、私はローカルマシン内のディレクトリ(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 "); 
} 

を、I SSH接続のおかげで、 "java.io.File(" ")"をUnixマシンへのパスで置き換える最後のコードの2行目を修正する必要があると考えてください。

SSH接続(例えば「pwd」コマンド)を使ってパスを取得すると、ローカルマシンではなくエクスプローラを開くために、2番目のコードをどのように適応させることができますか?

ありがとう、私は十分な情報を与えていないかどうか私に尋ねることを躊躇しないでください。

答えて

1

これは一時ディレクトリを作成し、my〜/ Documentsフォルダのファイル構造をtempディレクトリにコピーし、一時ディレクトリにファイル選択を表示します。これは本当に大まかですが、あなたがSSHでfindコマンドを実行するかどうかについて尋ねたことを行うはずです。

より堅牢なソリューションが必要な場合は、独自の実装を使用するか、Guavaに含まれるツールを使用してFileSystemを作成することを検討してください。 https://gist.github.com/prestongarno/b0034ae37ca5e757a35f996b4c72620d

:ここ

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

は私が作った要旨です

関連する問題