无法使用 JSch 将文件上传到 SFTP 服务器 - 没有此类文件 (SftpException)

问题描述 投票:0回答:1

我正在尝试连接到 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();


java sftp jsch
1个回答
4
投票

ChannelSftp.put

的第二个参数是
“远程目标文件名,而不是URL(也不仅仅是目标目录目录)。就像这样: String destinationPath = "/uploads/dummy.txt";

如果您不确定远程路径语法,请使用 GUI SFTP 客户端登录,然后查看它显示的路径。如果您的帐户没有 chroot,实际路径可能类似于 
/home/account/uploads/dummy.txt

    

© www.soinside.com 2019 - 2024. All rights reserved.