我正在使用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沒有? –