Reputation: 277
I am trying to connect to an SFTP server and upload a file. In my case I'm having to use a proxy and use private key for authentication. I was also given a specific location where the files need to be uploaded: sftp://mycompany.host.de/uploads
Below is my code snippet. I can establish a connection just fine and get authenticated as well via private key. But I face issues when trying to "put" transfer a file to remote server. I don't think I'm correctly defining this string value for the destination.
I've seen some examples online where a the username@host
is used to create some kind of URI, but I'm not sure how this is to be done. I've tried a few different things and cannot upload and I get a "No such file" exception. It can't be the source file, as this file exists.
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();
Upvotes: 2
Views: 6452
Reputation: 202222
The second argument of ChannelSftp.put
is "the remote destination file name", not a URL (but also not only a destination directory directory). So like this:
String destinationPath = "/uploads/dummy.txt";
If you are unsure of the remote path syntax, login with a GUI SFTP client, and see what path does it show. In case your account is not chrooted, the actual path might be like /home/account/uploads/dummy.txt
.
Upvotes: 4