我正在使用Java 1.7,JSCH 0.1.54和Commons-VFS2 2.1。
这是我正在使用的代码:
JSch.setConfig("kex", "diffie-hellman-group1-sha1,curve25519-sha256@libssh.org,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group14-sha1");
final StandardFileSystemManager manager = new StandardFileSystemManager();
try {
final FileSystemOptions opts = new FileSystemOptions();
FtpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, false);
FtpFileSystemConfigBuilder.getInstance().setPassiveMode(opts, true);
// 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);
manager.init();
for (final FileFTPHelper fileFTPHelper : filesHelper) {
final String filename = fileFTPHelper.getFilename();
final byte[] value = fileFTPHelper.getData();
final String remoteDestination = fileFTPHelper.getRemoteFolder() + filename;
String connection1 = null;
final String userInfo = fileFTPHelper.getUsername() + ":" + fileFTPHelper.getPassword();
URI uri = null;
try {
uri = new URI("sftp", userInfo, fileFTPHelper.getUrl(), fileFTPHelper.getPort(), remoteDestination, null, null);
connection1 = uri.toString();
} catch (final URISyntaxException e) {
throw new ValidationError("Cannot build the URL. Contact your administrator");
}
// Create remote file object
FileObject remoteFile = null;
this.log.trace("Sending file : " + remoteDestination);
try {
this.log.trace(connection1);
remoteFile = manager.resolveFile(connection1, opts);
} catch (final Exception e) {
this.log.error(e, "Error sfp");
}
OutputStream io = null;
if (remoteFile != null) {
remoteFile.refresh();
this.log.trace("Connected ! : " + remoteFile);
try {
io = remoteFile.getContent().getOutputStream();
io.write(value);
io.flush();
io.close();
} catch (final IOException e1) {
} finally {
if (io != null)
try {
io.close();
} catch (final Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
remoteFile.close();
if (!remoteFile.exists())
throw new ValidationError("Issue when sending files over sftp : The file has not been copied on the FTP");
if (!remoteFile.isFile())
throw new ValidationError("Issue when sending files over sftp : The file is not a file");
}
else
throw new ValidationError("Issue when sending files over sftp : Could not resolve file on the FTP");
System.out.println("File upload success");
}
} catch (final Exception e) {
this.log.error(e, "Error");
throw new ValidationError("Issue when sending files over sftp : " + e.getMessage());
} finally {
manager.close();
}
和我得到的输出:
SftpClientFactory(line 362) - Connecting to ftpqa.aaaa.com port 22
SftpClientFactory(line 362) - Connection established
SftpClientFactory(line 362) - Remote version string: SSH-2.0-Serv-U_15.1.6.26
SftpClientFactory(line 362) - Local version string: SSH-2.0-JSCH-0.1.54
SftpClientFactory(line 362) - CheckCiphers: aes256-ctr,aes192-ctr,aes128-ctr,aes256-cbc,aes192-cbc,aes128-cbc,3des-ctr,arcfour,arcfour128,arcfour256
SftpClientFactory(line 362) - aes256-ctr is not available.
SftpClientFactory(line 362) - aes192-ctr is not available.
SftpClientFactory(line 362) - aes256-cbc is not available.
SftpClientFactory(line 362) - aes192-cbc is not available.
SftpClientFactory(line 362) - CheckKexes: diffie-hellman-group14-sha1,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521
SftpClientFactory(line 362) - diffie-hellman-group14-sha1 is not available.
SftpClientFactory(line 362) - CheckSignatures: ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521
SftpClientFactory(line 362) - SSH_MSG_KEXINIT sent
SftpClientFactory(line 362) - SSH_MSG_KEXINIT received
SftpClientFactory(line 362) - kex: server: ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group14-sha1
SftpClientFactory(line 362) - kex: server: ssh-rsa
SftpClientFactory(line 362) - kex: server: aes256-cbc,rijndael256-cbc,rijndael-cbc@lysator.liu.se,aes256-ctr
SftpClientFactory(line 362) - kex: server: aes256-cbc,rijndael256-cbc,rijndael-cbc@lysator.liu.se,aes256-ctr
SftpClientFactory(line 362) - kex: server: hmac-sha2-256,hmac-sha2-256-96,hmac-sha2-512,hmac-sha2-512-96
SftpClientFactory(line 362) - kex: server: hmac-sha2-256,hmac-sha2-256-96,hmac-sha2-512,hmac-sha2-512-96
SftpClientFactory(line 362) - kex: server: zlib,none
SftpClientFactory(line 362) - kex: server: zlib,none
SftpClientFactory(line 362) - kex: server:
SftpClientFactory(line 362) - kex: server:
SftpClientFactory(line 362) - kex: client: ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group-exchange-sha1,diffie-hellman-group1-sha1
SftpClientFactory(line 362) - kex: client: ssh-rsa,ssh-dss,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521
SftpClientFactory(line 362) - kex: client: aes128-ctr,aes128-cbc,3des-ctr,3des-cbc,blowfish-cbc
SftpClientFactory(line 362) - kex: client: aes128-ctr,aes128-cbc,3des-ctr,3des-cbc,blowfish-cbc
SftpClientFactory(line 362) - kex: client: hmac-md5,hmac-sha1,hmac-sha2-256,hmac-sha1-96,hmac-md5-96
SftpClientFactory(line 362) - kex: client: hmac-md5,hmac-sha1,hmac-sha2-256,hmac-sha1-96,hmac-md5-96
SftpClientFactory(line 362) - kex: client: none
SftpClientFactory(line 362) - kex: client: none
SftpClientFactory(line 362) - kex: client:
SftpClientFactory(line 362) - kex: client:
SftpClientFactory(line 362) - Disconnecting from ftpqa.aaaa.com port 22
我有以下例外:
org.apache.commons.vfs2.FileSystemException - Could not connect to SFTP server at "sftp://***:***@ftpqa.aaaa.com/".
org.apache.commons.vfs2.FileSystemException - Could not connect to SFTP server at "ftpqa.aaaa.com".
com.jcraft.jsch.JSchException - Algorithm negotiation fail
我在各种网站上都看了一个,算法谈判失败似乎与缺少的算法有关。我添加了代码jsch.setConfig(" kex"," diffie-hellman-group1-sha1,curve25519-sha256@libssh.org,ecdh-sha2-nistp256,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-Hellman-group-exchange-sha256,difie-hellman-group14-sha1"(;但这无法正常工作。有什么想法吗?
我解决了问题,但下载Java密码扩展(JCE(无限强度管辖权策略文件,网址为http://www.oracle.com/technetwork/java/java/java/javase/javase/javase/jce-downloads/jce-jce-loads/jce-loads/jce-loads/jce-loads/7-download-432124.html并将其安装在Java-Home lib security