Recuperación de datos de un servidor SFTP usando JSch
Estoy usando JSch para recuperar un archivo de una máquina remota mediante SFTP. Aquí está el código
public class TestSFTPinJava {
public static void main(String args[]) {
JSch jsch = new JSch();
Session session = null;
try {
session = jsch.getSession("username", "sftp.abc.com", 22);
session.setConfig("StrictHostKeyChecking", "no");
session.setPassword("password");
session.connect();
Channel channel = session.openChannel("sftp");
channel.connect();
ChannelSftp sftpChannel = (ChannelSftp) channel;
System.out.println("Directory:" + sftpChannel.pwd());
sftpChannel.cd("remoteDirectory/");
System.out.println("Directory after cd:" + sftpChannel.pwd());
sftpChannel.get("remote-data.txt");
sftpChannel.put("C:\\Users\\mona\\Documents\\local-copy.txt");
sftpChannel.exit();
session.disconnect();
} catch (JSchException e) {
e.printStackTrace();
} catch (SftpException e) {
e.printStackTrace();
}
}
}
Ahora, tengo dos preguntas:
sftpChannel.get("remote-data.txt");
lanza una excepción:
No existe el archiv
at com.jcraft.jsch.ChannelSftp.throwStatusError (ChannelSftp.java:2297)
at com.jcraft.jsch.ChannelSftp._stat (ChannelSftp.java:1750)
at com.jcraft.jsch.ChannelSftp.get (ChannelSftp.java:1020)
at com.jcraft.jsch.ChannelSftp.get (ChannelSftp.java:995)
at TestSFTPinJava.main (TestSFTPinJava.java:29)
No estoy seguro de cómo especificar la ubicación en mi sistema local donde se guardará el archivo. @sftpChannel.put("C:\\Users\\mona\\Documents\\localCopy.txt");
no me parece correcto.
Por favor ayuda con sugerencias, ¡Gracias!