简体   繁体   English

无法启动到本地 apache sshd 服务器的 ChannelSftp 通道连接 (java)

[英]Cannot start a ChannelSftp channel connection to local apache sshd server (java)

I am trying to test files arriving in an SFTP server.我正在尝试测试到达 SFTP 服务器的文件。 I set up a local sshd server using the following code我使用以下代码设置了本地 sshd 服务器

    @Before
    public void setUp() throws Exception{
        SshServer sshd = SshServer.setUpDefaultServer();
        sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider());
        sshd.setPasswordAuthenticator(new PasswordAuthenticator() {
            public boolean authenticate(String username, String password, ServerSession session) {
                return true;
            }
        });
        sshd.setPort(22);
        sshd.setFileSystemFactory(new FileSystemFactory() {
            @Override
            public FileSystem createFileSystem(org.apache.sshd.common.session.Session session) throws IOException {
                return null;
            }
        });
        sshd.start();
    }

I use Jsch as a client to try and connect to this server.我使用 Jsch 作为客户端来尝试连接到该服务器。 Unfortunately, whenever I try to call channel.connect(), I get com.jcraft.jsch.JSchException: failed to send channel request exception.不幸的是,每当我尝试调用 channel.connect() 时,都会收到com.jcraft.jsch.JSchException: failed to send channel request异常。 The code for the Jsch is shown below: Jsch 的代码如下所示:

    public void testServer(){
        try{
            JSch jSch = new JSch();
            Session session = jSch.getSession("user", "localhost",22);
            Properties configTemp = new Properties();
            configTemp.put("StrictHostKeyChecking", "no");
            session.setConfig(configTemp);
            session.connect();
            ChannelSftp channel = (ChannelSftp)session.openChannel("sftp");
            channel.connect();
            if(channel.isConnected()){
                System.out.println("Connected");
            }
        }
        catch (Exception e){
            e.printStackTrace();
        }

    }

Can anyone help with what I am doing wrong and how to fix this?任何人都可以帮助解决我做错了什么以及如何解决这个问题? Thank you谢谢

您必须实现一个子系统工厂,例如:

sshd.setSubsystemFactories(Collections.singletonList(new SftpSubsystemFactory()));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM