使用JSCh在本地连接到SFTP服务器

时间:2017-09-18 21:03:03

标签: java server sftp jsch

我正在尝试在我的机器上本地与Bitvise(SFTP服务器)建立SFTP连接。我很难做到这一点。我想建立一个连接和下载上传文件。到目前为止这是我的代码。它会短暂连接然后断开连接。说无效的RSA模数。我想使用JSCH连接到SFTP服务器。除了我可以使用的Bitvise之外还有其他服务器吗?我究竟做错了什么?任何帮助表示赞赏。

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.security.KeyFactory;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.HostKey;
import com.jcraft.jsch.HostKeyRepository;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;

/**
 * @author kodehelp
 *
 */
public class SFTPinJava {

    /**
     *
     */
    public SFTPinJava() {
        super();
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        String SFTPHOST = "localhost";
        int SFTPPORT = 24;
        String SFTPUSER = "SftpUser";
        String SFTPPASS = "152207";
        String SFTPWORKINGDIR = "C:/SftpRoot";

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

        try {
            JSch jsch = new JSch();

            session = jsch.getSession(SFTPUSER, SFTPHOST, SFTPPORT);

            session.setPassword(SFTPPASS);

            java.util.Properties config = new java.util.Properties();
            config.put("StrictHostKeyChecking", "no");
            config.put("PreferredAuthentications", "password");
            session.setConfig(config);


            session.connect();
            channel = session.openChannel("ftp");
            channel.connect();
            channelSftp = (ChannelSftp) channel;
            channelSftp.cd(SFTPWORKINGDIR);
            byte[] buffer = new byte[1024];
            BufferedInputStream bis = new BufferedInputStream(channelSftp.get("Book1.xls"));
            File newFile = new File("C:/Book1.xls");
            OutputStream os = new FileOutputStream(newFile);
            BufferedOutputStream bos = new BufferedOutputStream(os);
            int readCount;
            while ((readCount = bis.read(buffer)) > 0) {
                System.out.println("Writing: ");
                bos.write(buffer, 0, readCount);
            }
            bis.close();
            bos.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }

}

我收到错误:

INFO: Connecting to localhost port 24
    INFO: Connection established
    INFO: Remote version string: SSH-2.0-7.35 FlowSsh: Bitvise SSH Server (WinSSHD) 7.35
    INFO: Local version string: SSH-2.0-JSCH-0.1.54
    INFO: CheckCiphers: aes256-ctr,aes192-ctr,aes128-ctr,aes256-cbc,aes192-cbc,aes128-cbc,3des-ctr,arcfour,arcfour128,arcfour256
    INFO: aes256-ctr is not available.
    INFO: aes192-ctr is not available.
    INFO: aes256-cbc is not available.
    INFO: aes192-cbc is not available.
    INFO: CheckKexes: diffie-hellman-group14-sha1,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521
    INFO: diffie-hellman-group14-sha1 is not available.
    INFO: ecdh-sha2-nistp256 is not available.
    INFO: ecdh-sha2-nistp384 is not available.
    INFO: ecdh-sha2-nistp521 is not available.
    INFO: CheckSignatures: ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521
    INFO: ecdsa-sha2-nistp256 is not available.
    INFO: ecdsa-sha2-nistp384 is not available.
    INFO: ecdsa-sha2-nistp521 is not available.
    INFO: SSH_MSG_KEXINIT sent
    INFO: SSH_MSG_KEXINIT received
    INFO: kex: server: curve25519-sha256,curve25519-sha256@libssh.org,ecdh-sha2-1.3.132.0.10,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group-exchange-sha1,diffie-hellman-group16-sha512,diffie-hellman-group15-sha512,diffie-hellman-group14-sha256,diffie-hellman-group14-sha1,diffie-hellman-group1-sha1,ext-info-s
    INFO: kex: server: rsa-sha2-512,rsa-sha2-256,ssh-rsa,ecdsa-sha2-nistp384
    INFO: kex: server: aes256-gcm@openssh.com,aes128-gcm@openssh.com,aes256-ctr,aes192-ctr,aes128-ctr,3des-ctr,aes256-cbc,aes192-cbc,aes128-cbc,3des-cbc
    INFO: kex: server: aes256-gcm@openssh.com,aes128-gcm@openssh.com,aes256-ctr,aes192-ctr,aes128-ctr,3des-ctr,aes256-cbc,aes192-cbc,aes128-cbc,3des-cbc
    INFO: kex: server: hmac-sha2-256,hmac-sha1
    INFO: kex: server: hmac-sha2-256,hmac-sha1
    INFO: kex: server: zlib,none
    INFO: kex: server: zlib,none
    INFO: kex: server: 
    INFO: kex: server: 
    INFO: kex: client: diffie-hellman-group-exchange-sha256,diffie-hellman-group-exchange-sha1,diffie-hellman-group1-sha1
    INFO: kex: client: ssh-rsa,ssh-dss
    INFO: kex: client: aes128-ctr,aes128-cbc,3des-ctr,3des-cbc,blowfish-cbc
    INFO: kex: client: aes128-ctr,aes128-cbc,3des-ctr,3des-cbc,blowfish-cbc
    INFO: kex: client: hmac-md5,hmac-sha1,hmac-sha2-256,hmac-sha1-96,hmac-md5-96
    INFO: kex: client: hmac-md5,hmac-sha1,hmac-sha2-256,hmac-sha1-96,hmac-md5-96
    INFO: kex: client: none
    INFO: kex: client: none
    INFO: kex: client: 
    INFO: kex: client: 
    INFO: kex: server->client aes128-ctr hmac-sha1 none
    INFO: kex: client->server aes128-ctr hmac-sha1 none
    INFO: SSH_MSG_KEX_DH_GEX_REQUEST(1024<1024<1024) sent
    INFO: expecting SSH_MSG_KEX_DH_GEX_GROUP
    INFO: SSH_MSG_KEX_DH_GEX_INIT sent
    INFO: expecting SSH_MSG_KEX_DH_GEX_REPLY
    INFO: Disconnecting from localhost port 24
    com.jcraft.jsch.JSchException: Session.connect: java.security.spec.InvalidKeySpecException: Unknown key spec: Invalid RSA modulus size.
        at com.jcraft.jsch.Session.connect(Session.java:565)
        at com.jcraft.jsch.Session.connect(Session.java:183)
        at SFTPinJava.main(SFTPinJava.java:57)

0 个答案:

没有答案