connect to SFTP using java

后端 未结 6 1669
野趣味
野趣味 2021-01-07 09:33

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.*;         


        
相关标签:
6条回答
  • 2021-01-07 10:10

    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 讨论(0)
  • 2021-01-07 10:14

    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();
    
    0 讨论(0)
  • 2021-01-07 10:15

    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.

    0 讨论(0)
  • 2021-01-07 10:25

    You can try increasing the timeout on Jsch Framework.

    session.connect(int Timeout)
    
    session.connect(30000);
    

    More on Jcsh javadoc

    0 讨论(0)
  • 2021-01-07 10:31

    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.

    0 讨论(0)
  • 2021-01-07 10:31

    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.

    0 讨论(0)
提交回复
热议问题