我写了一个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.8和 Diffie-Hellman参数大于 1024 位。要么将 JDK 版本更新到 1.8 或更高版本,要么在服务器端将 Diffie-Hellman 参数限制为 1024 位(具体操作方法取决于所使用的服务器软件,服务器配置工作是否更好,请参阅 Server Fault)。
关于java - 无法连接到 SFTP 服务器 Apache 常见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47754043/