You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Apache VFS/JSch连接SFTP时算法协商失败问题求助

问题:Apache VFS连接SFTP时算法协商失败的解决方法

我可通过命令行 sftp -oHostKeyAlgorithms=+ssh-dss user@host 正常连接SFTP服务器,但在Java Spring应用中使用Apache VFS连接时,出现如下异常:

ERROR] 20/06/2023 11:50:13.333 [http-nio-8080-exec-1]  o.a.c.c.C.[.[.[.[dispatcherServlet] - Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.RuntimeException: org.apache.commons.vfs2.FileSystemException: Could not connect to SFTP server at "sftp://user:***@host/".] with root cause
com.jcraft.jsch.JSchException: Algorithm negotiation fail

Apache VFS相关代码:

文件下载方法:

public void downloadFile(final String originFilePath, final String destinationFilePath) throws IOException {
        try (final StandardFileSystemManager manager = new StandardFileSystemManager()) {
            manager.init();
            final FileObject remoteFile = manager.resolveFile(getConnectionWithPath(originFilePath), createDefaultOptions());
            manager.resolveFile(destinationFilePath).copyFrom(remoteFile, Selectors.SELECT_SELF);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

获取连接路径方法:

public String getConnectionWithPath(final String remoteFilePath) {
        return "sftp://" + getUser() + ":" + getPassword() + "@" + getIp() + "/" + remoteFilePath;
    }

创建默认配置方法:

public static FileSystemOptions createDefaultOptions() throws FileSystemException {
        FileSystemOptions opts = new FileSystemOptions();

        SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, false);
        SftpFileSystemConfigBuilder.getInstance().setKeyExchangeAlgorithm(opts, "ssh-dss");
        SftpFileSystemConfigBuilder.getInstance().setDisableDetectExecChannel(opts, true);

        return opts;
    }

我使用的依赖版本为jsch 0.1.55和apache vfs 2.9.0。尝试多种方法无果后,我改用纯JSch实现了文件下载,相关代码如下:

纯JSch实现的文件下载代码:

下载方法:

public void downloadFile(String originFilePath, String destinationFilePath) throws IOException {
        try {
            final Session session = createSessionAndConnect();
            ChannelSftp sftpChannel = getJSCHConnectionChannel(session);

            sftpChannel.get(originFilePath, destinationFilePath);

            closeJSCHConnectionChannel(sftpChannel, session);
        } catch (JSchException | SftpException e) {
            e.printStackTrace();
        }
    }

创建并连接Session:

private Session createSessionAndConnect() throws JSchException {
        JSch jsch = new JSch();
        Session session = null;

        session = jsch.getSession(getUser(), getIp(), 22);
        session.setConfig("StrictHostKeyChecking", "no"); // Is this fine?
        session.setPassword(getPassword());
        session.connect();

        return session;
    }

获取SFTP通道:

private ChannelSftp getJSCHConnectionChannel(Session session) throws JSchException {
        Channel channel = session.openChannel("sftp");
        channel.connect();
        return (ChannelSftp) channel;
    }

关闭连接:

private void closeJSCHConnectionChannel(final ChannelSftp sftpChannel, final Session session) {
        sftpChannel.exit();
        session.disconnect();
    }

现求助如何解决Apache VFS连接SFTP时的算法协商失败问题?

内容的提问来源于stack exchange,提问作者StzEZ

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 17:37:47