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.*;
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);
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();
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.
You can try increasing the timeout on Jsch Framework.
session.connect(int Timeout)
session.connect(30000);
More on Jcsh javadoc
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.
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.