嗨,我想在 Linux 机器上编写一个 java 程序,它应该读取另一台远程 Linux 机器中的文件并将其内容复制到源机器。我正在使用以下代码
JSch jsch = new JSch();
Session session = jsch.getSession(user, host, port);
session.setPassword(password);
session.setConfig("StrictHostKeyChecking", "no");
System.out.println("Establishing Connection...");
session.connect();
System.out.println("Connection established.");
System.out.println("Crating SFTP Channel.");
ChannelSftp sftpChannel = (ChannelSftp) session.openChannel("sftp");
sftpChannel.connect();
System.out.println("SFTP Channel created.");
InputStream out = null; //.get(remoteFile);
out = sftpChannel.get(pub);
System.out.println("Read Successful");
System.out.println(pub);
StartString = pub.split("/");
i=StartString.length;
fileName =LocalWrite+StartString[i-1];
System.out.println(fileName);
OutputStream fileOut = new FileOutputStream(new File(fileName));
byte[] buf = new byte[1024];
int len;
while ((len = out.read(buf)) > 0) {
fileOut.write(buf, 0, len);
}
System.out.println("Wrote Successfull");
out.close();
fileOut.close();
sftpChannel.disconnect();
session.disconnect();`
当我尝试这个时,我得到一个 fileNotFound 异常,但是当我在 Windows 机器中尝试相同的代码时,我能够读取文件并将其内容复制到我的本地机器。你能告诉我我在哪里做错了吗?