我想顯示文件從Windows到java。我將ssh配置爲我的窗口,並嘗試獲取文件,我可以用從Windows獲取文件從ssh服務器從java
CD /程序文件(x86)/ syslogd的/日誌
TYPE SyslogCatchAll-2016-11-22.txt
讀取膩子文件public static void main(String[] args) throws JSchException,IOException,SftpException {
JSch jsch = new JSch();
Session s;
s = jsch.getSession("host", "192.168.1.2", 22);
s.setConfig("StrictHostKeyChecking", "no");
s.setPassword("host");
s.connect();
ChannelSftp sftpChannel = (ChannelSftp) s.openChannel("sftp");
sftpChannel.connect();
System.out.println("SFTP Channel created.");
InputStream out= null;
sftpChannel.cd("C:\\Program Files (x86)\\Syslogd\\Logs");
sftpChannel.get("SyslogCatchAll-2016-11-22.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(out));
String line;
while ((line = br.readLine()) != null)
{
System.out.println(line);
}
br.close();
sftpChannel.disconnect();'
輸出
SFTP Channel created.
Exception in thread "main" 2: No such file
at com.jcraft.jsch.ChannelSftp.throwStatusError(ChannelSftp.java:2873)
at com.jcraft.jsch.ChannelSftp._realpath(ChannelSftp.java:2367)
at com.jcraft.jsch.ChannelSftp.cd(ChannelSftp.java:342)
at fyp.Fyp.main(Fyp.java:53)
您的路徑不是絕對的。 'C:\'不在那裏。你怎麼錯過了:)? – Raghuveer
你的意思是像這樣的「C:/ Program Files(x86)/ Syslogd/Logs」,我只是試過還沒有工作 –