1

I need help with connection to SFTP server? Does anybody have working code?

I found something like this

package test.JSch;

import com.jcraft.jsch.*;

public class TestJSch {
public static void main(String args[]) {
    JSch jsch = new JSch();
    Session session = null;
    try {
        session = jsch.getSession("USSERNAME", "HOST", 22);
        //session.setConfig("StrictHostKeyChecking", "no");
        session.setPassword("PASSWORD");
        System.out.println("1");
        session.connect();
        System.out.println("2");

        Channel channel = session.openChannel("sftp");
        System.out.println("3");
        channel.connect();
        System.out.println("4");
        ChannelSftp sftpChannel = (ChannelSftp) channel;
        sftpChannel.get("remotefile.txt", "localfile.txt");
        sftpChannel.exit();
        session.disconnect();
    } catch (JSchException e) {
        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
    } catch (SftpException e) {
        e.printStackTrace();
    }
    }
}

but this was the output

com.jcraft.jsch.JSchException: java.net.ConnectException: Connection timed out: connect
    at com.jcraft.jsch.Util.createSocket(Util.java:258)
    at com.jcraft.jsch.Session.connect(Session.java:186)
    at com.jcraft.jsch.Session.connect(Session.java:145)
    at test.JSch.TestJSch.main(TestJSch.java:17)
Caused by: java.net.ConnectException: Connection timed out: connect
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.PlainSocketImpl.doConnect(Unknown Source)
    at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.<init>(Unknown Source)
    at java.net.Socket.<init>(Unknown Source)
    at com.jcraft.jsch.Util.createSocket(Util.java:252)
    ... 3 more

what is wrong with this code?

Ozair Kafray
  • 13,351
  • 8
  • 59
  • 84
skaryu
  • 141
  • 2
  • 3
  • 8
  • 1
    Can you be more specific when you say it doesn't work. What exception are you getting? Does it compile? What library are you using? –  Jun 09 '11 at 09:22

6 Answers6

2

You can try increasing the timeout on Jsch Framework.

session.connect(int Timeout)

session.connect(30000);

More on Jcsh javadoc

Njax3SmmM2x2a0Zf7Hpd
  • 1,354
  • 4
  • 22
  • 44
1

Here is an earlier question on Stackoverflow, for which the accepted answer suggests using JSch library.

How to retrieve a file from a server via SFTP?

I see that you have tried to connect via JSch and got an error.

I would suggest that the first thing is to check if you can connect to the sftp machine from the client (same machine on which you are testing your program), using a standard sftp client like Filezilla on Windows OR just the sftp command on a terminal in *nix systems.

Community
  • 1
  • 1
Ozair Kafray
  • 13,351
  • 8
  • 59
  • 84
0

JSch API : session.connect(); asking again user name and password to connect to server even though those are passed as parameter.

how to make the code to avoid asking user name and password again.

Ravish
  • 147
  • 1
  • 4
  • 17
0

I was having the same problem with a shell connection. The solution was to increase the timeout, in my case 5000 msec was enough

JSch jsch = new JSch();
Session session = jsch.getSession(USERNAME, HOSTNAME, SSH_PORT);
session.setPassword(PASSWORD);
Hashtable<String,String> config = new Hashtable<String,String>();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect(5000);
0

I've had success using the JSch library. It has notoriously bad documentation, but is rumored to implement its SSH functionalities very strictly and efficiently.

deltaforce2
  • 583
  • 2
  • 8
0

You can use Apache commons VFS

FileSystemManager fsManager = VFS.getManager();
FileObject remoteFile = fsManager.resolveFile("sftp://myusername:mypassword@somehost/pub/downloads/somefile.tgz" );
InputStream in = remoteFile.getContent().getInputStream();
Tony
  • 3,605
  • 14
  • 52
  • 84
KrzyH
  • 4,256
  • 1
  • 31
  • 43
  • Wasn't VFS quite a large bundle of different types of file management tools which all depend on several other jars? If the OP needs just the SFTP part, VFS would be a bit of overkill. – deltaforce2 Jun 09 '11 at 12:37
  • it appears to obsolete – Njax3SmmM2x2a0Zf7Hpd Dec 16 '13 at 17:17
  • http://code.google.com/p/otrosvfsbrowser/ is using Apache Commons VFS ver.2 to browser remote file systems including SFTP. It works just fine. – KrzyH Dec 18 '13 at 11:40