我正在使用java开发一个程序直接将文件下载到我的服务器,因为我正在使用jSch通过ssh连接并执行wget命令,但我想将输出流式传输到JTextArea,而我已经完成了,但它只会在下载完成时打印出来,我想将它作为wget命令的输出实时打印出来。
CustomOutputStream代码:
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);
}
这是Exec代码:
Fer = [32+float(9)/5*x for x in Cel]
答案 0 :(得分:0)
尝试{Thread.sleep(1000);} catch(Exception ee){}
您的代码可能正在Event Dispatch Thread (EDT)
上执行,并且Thread.sleep(...)阻止GUI重新绘制,直到任务完成执行。
因此,解决方案是为长时间运行的任务启动一个单独的线程,以便EDT能够响应事件。
一种简单的方法是使用SwingWorker
。阅读Concurrency in Swing上的Swing教程中的部分。该教程详细介绍了EDT
和SwingWorker
。
答案 1 :(得分:0)
经过一些帮助后,我只需要创建一个新的线程处理器连接。
以下是我采取的示例: 从这个链接:
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();