Reputation: 257
I am trying to fetch a file from Linux/Windows to a windows system, and I have written a code for this. But it seems there is some problem, either with the code or Jsch itself.
String host = "<IP>";
int port = Integer.parseInt("22");
String userName = "<USERNAME>";
String password = "<PASSWORD>";
ChannelSftp sftpChannel = null;
try {
JSch jsch = new JSch();
Session session = jsch.getSession(userName, host, port);
session.setConfig("StrictHostKeyChecking", "no");
session.setPassword(password);
session.connect();
// open an SFTP channel
Channel channel = session.openChannel("sftp");
channel.connect();
sftpChannel = (ChannelSftp) channel;
File headerFolder = new File("/home/userName/testFiles");
if (!headerFolder.exists()) {
headerFolder.mkdirs();
}
sftpChannel.get("/d/myfile.txt", "/home/username/aFolder"); //windows to linux
} catch (JSchException e) {
//LOGGER.error(e.getMessage(), e);
e.printStackTrace();
} catch (SftpException e) {
//LOGGER.error(e.getMessage(), e);
e.printStackTrace();
}
2: The file does not exist.
at com.jcraft.jsch.ChannelSftp.throwStatusError(ChannelSftp.java:2846)
at com.jcraft.jsch.ChannelSftp._stat(ChannelSftp.java:2198)
at com.jcraft.jsch.ChannelSftp._stat(ChannelSftp.java:2215)
at com.jcraft.jsch.ChannelSftp.get(ChannelSftp.java:913)
at com.jcraft.jsch.ChannelSftp.get(ChannelSftp.java:873)
Upvotes: 0
Views: 4673
Reputation: 102
You need to perform change directory before listing out the files. I doesn't make sense but it worked for me.
sftpChannel = (ChannelSftp) channel;
sftpChannel.cd(folder/subfolder);
Upvotes: 0
Reputation: 25448
sftpChannel.get("/d/myfile.txt","/home/username/aFolder");
2: The file does not exist.
Your client tried to get "/d/myfile.txt", from the SFTP server, and the server responded that the file doesn't exist. You indicate the remote server is a Windows system, so I presume you're trying to get "D:\myfile.txt". It seems there are three possibilities here:
The SFTP protocol uses a unix-like model for file pathnames, so one would expect to use "/" as a separator and for absolute filenames to start with "/". If "/d/myfile.txt" isn't the right path to get the file, you should check the documentation for the SFTP server and/or ask the server administrator how to access the root of the D: drive through SFTP.
Alternately, you could try logging into the server with an interactive SFTP client and seeing what files are visible. If you were to cd to the / directory and start looking around, the correct path to the remote file may become obvious.
Upvotes: 1