1

I want to transfer a file through SFTP. I found a similar question that is asked from how to transfer a file through SFTP in java?. I tried out the suggested solution from the post on localhost. but I get the following error shown under output.

output

preparing the host information for sftp.
Host connected.
sftp channel opened and connected.
2: No such file
at com.jcraft.jsch.ChannelSftp.throwStatusError(ChannelSftp.java:2846)
at com.jcraft.jsch.ChannelSftp._realpath(ChannelSftp.java:2340)
at com.jcraft.jsch.ChannelSftp.cd(ChannelSftp.java:342)
at ScpTo.main(ScpTo.java:69)
File transfered successfully to host.
sftp Channel exited.
Channel disconnected.
Host Session disconnected.

Code

import java.io.File;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;

public class ScpTo {

    /**
     *
     */
    public ScpTo() {
        // TODO Auto-generated constructor stub
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
         String SFTPHOST = "192.168.1.3";
            int SFTPPORT = 22;
            String SFTPUSER = "sam-PC";
            String SFTPPASS = "";
            String SFTPWORKINGDIR = "C:/Users/sam/Desktop/hi.txt";

            Session session = null;
            Channel channel = null;
            ChannelSftp channelSftp = null;
            System.out.println("preparing the host information for sftp.");

                JSch jsch = new JSch();
                try {
                    session = jsch.getSession(SFTPUSER, SFTPHOST, SFTPPORT);
                } catch (JSchException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                session.setPassword(SFTPPASS);
                java.util.Properties config = new java.util.Properties();
                config.put("StrictHostKeyChecking", "no");
                session.setConfig(config);
                try {
                    session.connect();
                } catch (JSchException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                System.out.println("Host connected.");
                try {
                    channel = session.openChannel("sftp");
                } catch (JSchException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                try {
                    channel.connect();
                } catch (JSchException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                System.out.println("sftp channel opened and connected.");
                channelSftp = (ChannelSftp) channel;
                try {
                    channelSftp.cd(SFTPWORKINGDIR);
                } catch (SftpException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                File f = new File(SFTPWORKINGDIR);
                try {
                    channelSftp.put(new FileInputStream(f), f.getName());
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (SftpException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                System.out.println("File transfered successfully to host.");


                channelSftp.exit();
                System.out.println("sftp Channel exited.");
                channel.disconnect();
                System.out.println("Channel disconnected.");
                session.disconnect();
                System.out.println("Host Session disconnected.");

    }
}
Community
  • 1
  • 1
user2211678
  • 669
  • 8
  • 13
  • 24

3 Answers3

5

The stack-trace shows that the error happened during execution of the cd command. The error is "no such file", which probably actually means "no such directory", so the problem is almost certainly that the directory defined by SFTPWORKINGDIR doesn't exist.

I don't blame you for being confused, though. That API seems to be hellishly complicated. I'm one of the developers of edtFTPj/PRO, which offers a much simpler interface. For example, here's code to download a file over SFTP:

SecureFileTransferClient client = new SecureFileTransferClient();
client.setProtocol(Protocol.SFTP);
client.setRemoteHost("myserver.com");
client.setUserName("myusername");
client.setPassword("mypassword");
client.connect();
client.downloadFile("filename.txt", "filename.txt");
client.disconnect();

Note that edtFTPj/PRO is not a free product.

HansA
  • 1,375
  • 2
  • 11
  • 19
4
String SFTPWORKINGDIR = "C:/Users/sam/Desktop/hi.txt";
...
channelSftp.cd(SFTPWORKINGDIR);

To start with, SFTPWORKINGDIR appears to refer to a file, not a directory. You may be trying to cd to something that's a file, or that's supposed to be a file.

Second, SFTP uses a unix-like model for pathnames. Pathnames which begin with "/" are absolute paths and are resolved from the server's root directory. Paths which don't begin with "/" are relative paths, and are resolved from the directory where your SFTP session started (usually the home directory for the account that you logged into on the server).

Your value of SFTPWORKINGDIR is a windows-format path, not a unix-format path. It's very likely that the SFTP server isn't interpreting it as you intend, even if that SFTP server is running on a Windows system. You should consult the documentation for your SFTP server and convert the pathname into the proper form to access the directory that you want. If the SFTP server is the Cygwin openssh server, I believe the right form will be something like "/cygdrive/c/Users/sam/Desktop".

Kenster
  • 23,465
  • 21
  • 80
  • 106
0

Here is a complete 'upload code' with authentication from a windows machine to linux machine. this code uploads file from specific location from your machine(win) and uploads to FTP server. This is my solution that i applied for my problem. if somebody sees problem, please comment.

try {
        JSch jsch = new JSch();
        Session session = jsch.getSession( "<userName>", "<ip_block>", 22 );
        session.setConfig( "PreferredAuthentications", "password" );
        session.setPassword( "<pasword>" );
        java.util.Properties config = new java.util.Properties(); 
        config.put("StrictHostKeyChecking", "no");//do not prefer this. demo only
        session.setConfig(config);
        session.connect( 1200 );
        Channel channel = session.openChannel( "sftp" );
        ChannelSftp sftp = ( ChannelSftp ) channel;
        sftp.connect( 600 );
        channel = session.openChannel("sftp");
        channel.connect();
        try {
            File f = new File("E:/apache-tomcat-7.0.65/bin/" + this.fileName);
            sftp.put(new FileInputStream(f), f.getName());
        } catch (Exception e) {
            e.printStackTrace();
        }
        channel.disconnect();
        sftp.disconnect();
    } catch (JSchException e) {
        e.printStackTrace();
    }
comitatenses
  • 15
  • 2
  • 5