我已经搜索了很多,但无法找到解决方案。我需要使用 java 程序将文件从本地 Windows 机器复制到远程 Windows 机器。我试过 JSch,
JSch jsch = new JSch();
Session session = null;
session = jsch.getSession("username","hostname",22);
session.setPassword("password");
session.setConfig("StrictHostKeyChecking", "no");
session.connect();
ChannelSftp channel = null;
channel = (ChannelSftp)session.openChannel("sftp");
channel.connect();
File localFile = new File("filePath");
//If you want you can change the directory using the following line.
channel.cd("E:/xxx");
channel.put(new FileInputStream(localFile),localFile.getName());
channel.disconnect();
session.disconnect();
在执行上面的代码时,我遇到了以下错误,
Exception in thread "main" 2: No such file
at com.jcraft.jsch.ChannelSftp.throwStatusError(ChannelSftp.java:2846)
at com.jcraft.jsch.ChannelSftp._realpath(ChannelSftp.java:2340)
at com.jcraft.jsch.ChannelSftp.cd(ChannelSftp.java:342)
我在远程 Windows 机器上安装了 cygwin。似乎 Jsch 无法找到 Windows 路径。将文件从 windows 机器复制到 linux 机器时,相同的代码可以正常工作。
请告诉我上述问题的解决方案,或者是否有任何其他选项可以在 java 中实现?谢谢
最佳答案
为了使用盘符解析 Windows 路径,您可能需要使用 /cygdrive
prefix .在这种情况下,您的 cd
方法调用应使用参数 /cygdrive/e/xxx
调用。
关于java - 使用java将文件从本地窗口机器复制到远程窗口机器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37278865/