Java 带有进度条和实时输出到 JTextArea 的 jSch Exec

Java jSch Exec with progress bar and real time output to a JTextArea

我正在 java 中开发一个程序来直接将文件下载到我的服务器,为此我使用 jSch 通过 ssh 连接并执行 wget 命令,但我想流式传输输出到JTextArea,我已经这样做了,但是它只会在下载完成时打印它,我想把它作为wget命令的输出实时打印出来。

CustomOutputStream 代码:​​

public class CustomOutputStream extends OutputStream{
private JTextArea textArea;

    public CustomOutputStream(JTextArea textArea) {
        this.textArea = textArea;
    }

    @Override
    public void write(int b) throws IOException {
        // redirects data to the text area
        textArea.append(String.valueOf((char) b));
        // scrolls the text area to the end of data
        textArea.setCaretPosition(textArea.getText().length());
    }

    @Override
    public void write(byte[] b, int off, int len) throws IOException {
        String s = new String(b,off,len);
        textArea.append(s);
        textArea.setCaretPosition(textArea.getText().length());
    }

    @Override
    public void write(byte[] b) throws IOException {
        this.write(b, 0, b.length);
    }

}

这是执行代码:

Properties props = new Properties(); 
    props.put("StrictHostKeyChecking", "no");
    int port = 22;

    JSch jsch=new JSch();  

    Session session=jsch.getSession(username, host, 22);
    session.setConfig(props);
    session.setPassword(password);
    session.connect();
    String command =  "cd " + info.downPath + " && printf \"" + downLinks + "\" l> d.txt && wget -i d.txt";
    Channel channel=session.openChannel("exec");
    InputStream in=channel.getInputStream();
    OutputStream out = channel.getOutputStream();
    ((ChannelExec)channel).setCommand(command);
    //((ChannelExec)channel).setErrStream(System.err);
    //((ChannelExec)channel).setOutputStream(System.out);
    ((ChannelExec)channel).setErrStream(new CustomOutputStream(taOutput));
    //((ChannelExec)channel).setOutputStream(new CustomOutputStream(taOutput));
    channel.connect();
    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();
    bar.setValue(100);

尝试{Thread.sleep(1000);}捕捉(异常 ee){}

您的代码可能正在 Event Dispatch Thread (EDT) 上执行,并且 Thread.sleep(...) 阻止 GUI 在任务完成执行之前重新绘制自身。

所以解决方案是为长 运行 任务启动一个单独的线程,以便 EDT 能够响应事件。

一个简单的方法是使用 SwingWorker。阅读 Concurrency in Swing 上的 Swing 教程部分。本教程详细解释了 EDTSwingWorker

经过一些帮助和更多搜索后,我创建了一个新线程来处理连接。

这是我举的例子: 从这个 link :

one = new Thread() {
public void run() {
    try {
        System.out.println("Does it work?");

        Thread.sleep(1000);

        System.out.println("Nope, it doesnt...again.");
    } catch(InterruptedException v) {
        System.out.println(v);
    }
}  

};

one.start();