我正在尝试连接到 SFTP 服务器并上传文件。就我而言,我必须使用代理并使用私钥进行身份验证。我还获得了需要上传文件的特定位置:
sftp://mycompany.host.de/uploads
下面是我的代码片段。我可以很好地建立连接并通过私钥进行身份验证。但是当我尝试将文件“放置”传输到远程服务器时,我遇到了问题。 我认为我没有正确定义目的地的这个字符串值。
我在网上看到了一些示例,其中使用
username@host
创建某种 URI,但我不确定这是如何完成的。我尝试了一些不同的方法,但无法上传,并且出现“没有这样的文件”异常。它不可能是源文件,因为该文件存在。
JSch jsch = new JSch();
jsch.addIdentity("path\\to\\privateKey"); // using private key authentication
session = jsch.getSession("myUser", "mycompany.host.de");
// (I understand the security threat here)
session.setConfig("StrictHostKeyChecking", "no");
ProxySOCKS5 proxy = new ProxySOCKS5("mycompany.host.de", 8080);
proxy.setUserPasswd("myUser", null);
session.setProxy(proxy);
session.connect();
channel = (ChannelSftp) session.openChannel("sftp");
channel.connect();
// this file does exist and I can retrieve it just fine
String sourcePath = "test/dummy.txt";
// issue is here not to sure how set the "destination" properly
// (the remote destination file name)
String destinationPath = "sftp://mycompany.host.de/uploads/dummy.txt";
// "/mycompany.host.de/uploads/dummy.txt";
// EXCEPTION is thrown here, SftpException NO SUCH FILE.
channel.put(sourcePath, destinationPath);
// do more stuff....
channel.disconnect();
channel.exit();
session.disconnect();