开发者

connect to SFTP using java

开发者 https://www.devze.com 2023-03-11 06:49 出处:网络
I need help with connection to SFTP server? Does anybody have working code? I found something like this

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");
        sftp开发者_运维技巧Channel.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?


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.


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.


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号