java - 无法连接到 SFTP 服务器 Apache 常见

标签 java apache file-upload sftp pgp

我写了一个java代码(使用apache common vfs2)来上传文件到SFTP服务器。最近,我在我的服务器上引入了 PGP 安全性。现在,java 代码无法连接到该服务器。与 FileZilla 连接成功。我们在服务器上使用 CrushFTP,在 java 应用程序中使用 apache-common-vfs2。这是代码片段

String originalFileName = localFile.getName();
manager.init();
FileObject fileToUpload = manager.resolveFile(localFile.getAbsolutePath());

// Create remote file object
FileObject remoteFile = manager.resolveFile(
              createConnectionString(originalFileName),
               createDefaultFileSystemOptions());

remoteFile.copyFrom(fileToUpload, Selectors.SELECT_SELF);

方法

public String createConnectionString(String fileName) {
    String path = "sftp://" + username + ":" + password + "@" + server +workingDir+"/"+fileName;
    logger.info("uploading file at "+path);
    return path;
}

public static FileSystemOptions createDefaultFileSystemOptions()
                            throws FileSystemException {
    // Create SFTP options
    FileSystemOptions opts = new FileSystemOptions();

    // SSH Key checking
    SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(opts, "no");

   // Root directory set to user home
   SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, false);

   // Timeout is count by Milliseconds
   SftpFileSystemConfigBuilder.getInstance().setTimeout(opts, 10000);
   return opts;
}

异常情况如下

Caused by: org.apache.commons.vfs2.FileSystemException: Could not connect to SFTP server at "192.168.13.102".
    at org.apache.commons.vfs2.provider.sftp.SftpClientFactory.createConnection(SftpClientFactory.java:170)
    at org.apache.commons.vfs2.provider.sftp.SftpFileProvider.doCreateFileSystem(SftpFileProvider.java:97)
    ... 16 more
Caused by: com.jcraft.jsch.JSchException: Session.connect: java.security.InvalidAlgorithmParameterException: Prime size must be multiple of 64, and can only range from 512 to 1024 (inclusive)
    at com.jcraft.jsch.Session.connect(Session.java:565)

有人请建议解决方案吗?

最佳答案

错误消息表明您正在使用 Java release older than 1.8Diffie-Hellman参数大于 1024 位。要么将 JDK 版本更新到 1.8 或更高版本,要么在服务器端将 Diffie-Hellman 参数限制为 1024 位(具体操作方法取决于所使用的服务器软件,服务器配置工作是否更好,请参阅 Server Fault)。

关于java - 无法连接到 SFTP 服务器 Apache 常见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47754043/

相关文章:

java - 为什么 Spring @Value 与 @Controller 不兼容?

java - 插入语句的Postgresql错误代码

mysql - 对 MySQL 的攻击——以及如何防止它?

java - “发生Java异常” Apache POI错误

asp.net - context.Request.Files 集合仅在远程服务器上为空

node.js - 为什么我可以使用绝对路径而不是相对路径将 Formidable 文件上传到文件夹?

java - 如何获取android.jar的源代码

java - 安卓/Java : How To Stop Download After 5 Seconds?

apache - Htaccess 重定向 301 从 "with parameter"到 "without parameter"

php - AjaxFileUpload 插件无法检索 $_POST 或 $_FILES 数据