1

I am trying to use localhost as SFTP server. I'm using Jsch library of Java to implement SFTP with SSH2. The following code upload a text file to a directory on local machine with sftp. But I cannot connect to localhost.

        import java.io.BufferedInputStream;
        import java.io.BufferedOutputStream;
        import java.io.File;
        import java.io.FileInputStream;
        import java.io.FileOutputStream;
        import java.io.OutputStream;


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


        public class  Server{

        public Server() {
        }

        public static void main(String[] args) {
        String SFTPHOST = "localhost";
        int    SFTPPORT = 22;
        String SFTPUSER = "root";
        String SFTPPASS ="";
        String SFTPWORKINGDIR = "D:\\Upload";

        Session     session     = null;
        Channel     channel     = null;
        ChannelSftp channelSftp = null;

        try{
                    JSch jsch = new JSch();
                    session = jsch.getSession(SFTPUSER,SFTPHOST,SFTPPORT);
                    session.setPassword(SFTPPASS);
                    java.util.Properties config = new java.util.Properties();
                    config.put("StrictHostKeyChecking", "no");
                    session.setConfig(config);
                    session.connect();
                    channel = session.openChannel("sftp");
                    channel.connect();
                    channelSftp = (ChannelSftp)channel;
                    channelSftp.cd(SFTPWORKINGDIR);
                    File f = new File("trial.txt");
                    channelSftp.put(new FileInputStream(f), f.getName());
        }catch(Exception ex){
        ex.printStackTrace();
        }

        }

        }
Sam
  • 335
  • 1
  • 6
  • 13

1 Answers1

0

"Connection refused" means that there is no process listening for connections at the IP address and port which you're trying to connect to. In this case, the most likely explanation is that your SSH server process isn't running, or perhaps it has been configured to listen on some other port than port 22.

It looks like this is a Windows system? If that's the case, you could check the task manager to see if there is a copy of the sshd program running. To see if something is listening on port 22, open a command-line window and run netstat -an. Then look for a line like this:

  TCP    0.0.0.0:22            0.0.0.0:0              LISTENING
                 ^^-- Listening on port 22

If sshd isn't running, it needs to be started. If it is running, but nothing is listening on port 22, then you should examine sshd's configuration file.

If you need further help starting sshd, you should ask on https://superuser.com/.

Community
  • 1
  • 1
Kenster
  • 23,465
  • 21
  • 80
  • 106