3

Following is my program...but this is not working after session.connect()

public static void main(String args[])
        {
        try {
            String ftpHost = "XXXXXXXX";
            int ftpPort = 21;     
            String ftpUserName = "XXXX";
            String ftpPassword = "XXXXX";
            String ftpRemoteDirectory = "/";
            String fileToTransmit = "C://XXXXX//Desktop//RG//10171699_821972117859158_5724612734096298046_n.jpg";          
            JSch.setLogger(new MyLogger());
            System.out.println("Creating session.");
            JSch jsch = new JSch();

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

            //
            // Now connect and SFTP to the SFTP Server
            //
            try {
                // Create a session sending through our username and password
                session = jsch.getSession(ftpUserName, ftpHost, ftpPort);
                System.out.println("Session created.");
                session.setPassword(ftpPassword);

                java.util.Properties config = new java.util.Properties();
                config.put("StrictHostKeyChecking", "no");
                session.setConfig(config);
                System.out.println("Session connected before.");
                session.connect();
                System.out.println("Session connected.");

                System.out.println("OPEN SFTP CHANNEL");
                //
                // Open the SFTP channel
                //
                System.out.println("Opening Channel.");
                channel = session.openChannel("sftp");
                channel.connect();
                c = (ChannelSftp) channel;
                System.out.println("Now checing status");
            } catch (Exception e) {
                System.err.println("Unable to connect to FTP server."
                        + e.toString());
                throw e;
            }

            //
            // Change to the remote directory
            //
            System.out.println("Now performing operations");
            ftpRemoteDirectory="/home/pooja111/";
            System.out.println("Changing to FTP remote dir: "
                    + ftpRemoteDirectory);
            c.cd(ftpRemoteDirectory);

            //
            // Send the file we generated
            //
            try {
                File f = new File(fileToTransmit);
                System.out.println("Storing file as remote filename: "
                        + f.getName());
                c.put(new FileInputStream(f), f.getName());
            } catch (Exception e) {
                System.err
                        .println("Storing remote file failed." + e.toString());
                throw e;
            }   

            //
            // Disconnect from the FTP server
            //
            try {
                c.quit();
            } catch (Exception exc) {
                System.err.println("Unable to disconnect from FTPserver. "
                        + exc.toString());
            }

        } catch (Exception e) {
            System.err.println("Error: " + e.toString());
        }

        System.out.println("Process Complete.");
        System.exit(0);
    }

and the output is

Creating session.
Session created.
Session connected before.
INFO: Connecting to ftp.olstr.com port 21
INFO: Connection established

My code control is not moving after session.connect() line.

Pedantic
  • 1,368
  • 10
  • 39
  • 70
  • possible duplicate of [JSCH: SFTP. Hangs at session.connect() using the port 21](http://stackoverflow.com/questions/20559448/jsch-sftp-hangs-at-session-connect-using-the-port-21) – Bhesh Gurung May 09 '14 at 04:59
  • The server you are trying to connect is `ftp` or `sftp`? – earthmover May 09 '14 at 05:04
  • Default ports: `ftp = 21, sftp = 22` – Wundwin Born May 09 '14 at 05:15
  • I am using FTP Server.my SFTP Server port is 3335..but when i try that i recieve Creating session. Session created. Session connected before. INFO: Connecting to ftp.olstr.com port 3335 Unable to connect to FTP server.com.jcraft.jsch.JSchException: java.net.ConnectException: Connection timed out: connect Error: com.jcraft.jsch.JSchException: java.net.ConnectException: Connection timed out: connect Process Complete. – Pedantic May 09 '14 at 05:32
  • I think you are using ftps instead of sftp. ftps is ftp over ssl, it is totally different to sftp which runs on ssh port. – Ted Shaw May 09 '14 at 05:46
  • Very useful code in the question: saved me a heap of time, thanks. ;) – Tomislav Nakic-Alfirevic Feb 24 '16 at 14:40

1 Answers1

6

There are different ports for different connections like FTP, FTPS, SFTP, FTP over SSH. Use the appropriate port. These are the ports. 20 FTP data (File Transfer Protocol) ,21 FTP (File Transfer Protocol) ,22 SSH (Secure Shell) . Use session.connect(timeout) . Comment your stack trace so that i'll know what is the exact error. Try with 22 and see if the error still persists.

Tejus Prasad
  • 6,322
  • 7
  • 47
  • 75