我正在使用JSch通过SSH连接到服务器,并使用传输文件的扫描命令。当第二次调用时,JSch不提供输出
该代码已经过测试并且运行良好。我把所有的输出都传给了命令行。然而 - 当我再次使用相同的方法时,一切仍然“有效”,但我没有看到任何输出。我需要输出来查看是否一切正常。
在当一个按钮被按下发生这种情况的主要应用:
Scan s = new Scan(getHost(),getUser(),getPassword());
s.scan("scanscript \"" + getScancommand() + "\" \"" + getFilename() + "\"");
scanscript
需要两个参数:一个是全命令scanimage blah blah
- 另一种是所需的文件名。 Scanscript也返回一个需要的退出代码 - 但它并不总是获得。
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import java.io.InputStream;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.JSchException;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonType;
public class Scan {
private final String scan_host;
private final String scan_user;
private final String password;
public Scan(String host, String user, String password) {
this.scan_host = host;
this.scan_user = user;
this.password = password;
}
public void scan(String command) {
boolean b_success = true;
try {
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
JSch jsch = new JSch();
Session session = jsch.getSession(scan_user, scan_host, 22);
session.setPassword(password);
session.setConfig(config);
session.connect();
Logger.getLogger(Scan.class.getName()).log(Level.INFO, "Command: {0}", command);
System.out.println("Connected"); //This here is displayed always
Channel channel = session.openChannel("exec");
((ChannelExec) channel).setCommand(command);
channel.setInputStream(null);
((ChannelExec) channel).setErrStream(System.err);
InputStream in = channel.getInputStream();
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 (channel.getExitStatus() != 0) {
b_success = false;
javafx.scene.control.Alert alert = new Alert(Alert.AlertType.ERROR, "Scan failed\n - Exit-Status: " + channel.getExitStatus(), ButtonType.OK);
alert.showAndWait();
}
System.out.println("exit-status: " + channel.getExitStatus());
break;
}
try {
Thread.sleep(1000);
} catch (InterruptedException ee) {
Logger.getLogger(Scan.class.getName()).log(Level.SEVERE, null, ee);
}
}
channel.disconnect();
session.disconnect();
if (b_success) {
javafx.scene.control.Alert alert = new Alert(Alert.AlertType.CONFIRMATION, "Scan successful", ButtonType.OK);
alert.showAndWait();
}
} catch (JSchException | IOException e) {
Logger.getLogger(Scan.class.getName()).log(Level.SEVERE, null, e);
javafx.scene.control.Alert alert = new Alert(Alert.AlertType.ERROR, "Scan failed: " + e.getLocalizedMessage(), ButtonType.OK);
alert.showAndWait();
}
}
}
这里是我的控制台输出:
Jun 15, 2017 9:12:03 PM com.schwaiger.kanva.scan.Scan scan
INFORMATION: Befehl: scanscript "scanimage --device='brother4:net1;dev0' --format tiff --resolution=150 --source 'Automatic Document Feeder(left aligned,Duplex)' -l 0mm -t 0mm -x210mm -y297mm --batch=$(date +%Y%m%d_%H%M%S)_p%04d.tiff" "testscan1.pdf"
Connected
scanimage: rounded value of br-x from 210 to 209.981
scanimage: rounded value of br-y from 297 to 296.973
Scanning -1 pages, incrementing by 1, numbering from 1
Scanning page 1
Scanned page 1. (scanner status = 5)
Scanning page 2
Scanned page 2. (scanner status = 5)
Scanning page 3
Scanned page 3. (scanner status = 5)
Scanning page 4
Scanned page 4. (scanner status = 5)
Scanning page 5
scanimage: sane_start: Document feeder out of documents
exit-status: 0
Connected
exit-status: 0
如你我第一次叫我这是怎么回事对整个信息的命令后看到。第二次,我刚刚得到Connected
和退出状态。但我无法确定这种状态是指我的剧本还是整个操作。
你从哪里得到输出?在输出或错误流? +你的意思是“退出代码是必需的 - **但它并不总是**。”? –
@MartinPrikryl - 编辑。输出只是普通的Java控制台输出 – Qohelet
我的意思是如果你在JSch标准或错误输出中获得命令输出 - 如果你注释掉了((ChannelExec)channel).setErrStream(System.err);'你还在得到输出ot没有? –