Retrieving data from an SFTP server using JSch

前端 未结 3 631
孤城傲影
孤城傲影 2021-02-14 17:12

I am using JSch for retrieving a file from a remote machine by SFTP. Here is the code

public class TestSFTPinJava {

 public static void main(String args[]) {
           


        
3条回答
  •  悲&欢浪女
    2021-02-14 17:39

    i had a similar issue, i was trying to get some files from a server where everything was fine, but i was getting always this error:

    sftpChannel.get("fil1.txt","file1.txt")
    
    error message: 
    
    2: No such file
    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)
    ...
    

    I was listing properly all the elements of the directory by using

    java.util.Vector v1 = sftpChannel.ls(dir);
    

    I think that was that confused me, that i was able to read the content of the directory by using the ls command, when you want to get / put files make sure you move first by using "cd".

    The solution was to use the next command to move to the directory that contains my files using a simple cd command:

    sftpChannel.cd(dir);
    

    Hope this helps, i took my sometime to figure it out. jojo.

    Lessons learned:

    1.- ls can read any directory no matter if you are not inside of it. 2.- To get and put files always make sure you are in the directory that contains the files by using cd.

提交回复
热议问题