鏈接SFTP服務器時老是拋出如下異常,並且本身也在本地測試800多遍了都沒有任何問題,就是在Linux平臺下出現的
org.apache.commons.vfs.FileSystemException: Could not connect to SFTP server at "168.22.73.69".
at org.apache.commons.vfs.provider.sftp.SftpClientFactory.createConnection(SftpClientFactory.java:214)
at com.wxcking.admins.util.sftp.SFTPUtil.connectServer(SFTPUtil.java:270)
at com.wxcking.admins.util.sftp.SFTPUtil.<init>(SFTPUtil.java:66)
at com.wxcking.admins.util.SftpConnectUtil.connectSupBankSftp(SftpConnectUtil.java:36)
at com.wxcking.admins.redeem.service.RedemptionBankService.uploadRegulatoryBank(RedemptionBankService.java:285)
at com.wxcking.admins.redeem.service.RedemptionBankService$$FastClassByCGLIB$$864fe42a.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:627)
at com.wxcking.admins.redeem.service.RedemptionBankService$$EnhancerByCGLIB$$b98834a5.uploadRegulatoryBank(<generated>)
at com.wxcking.manage.contract.service.adminsRedeemCollectService.processCollect(adminsRedeemCollectService.java:89)
... ...
Caused by: com.jcraft.jsch.JSchException: Auth fail
at com.jcraft.jsch.Session.connect(Session.java:464)
at com.jcraft.jsch.Session.connect(Session.java:158)
at org.apache.commons.vfs.provider.sftp.SftpClientFactory.createConnection(SftpClientFactory.java:210)
... 111 more
這種異常主要是看Caused by後面的 com.jcraft.jsch.JSchException: Auth fail,報的是 Caused by: com.jcraft.jsch.JSchException: Auth fail驗證失敗。另外,使用FlashFXP等工具鏈接SFTP時都沒有問題,使用Java寫的程序鏈接出現Auth fail異常。
從網上查詢到解決的方法:
第一種說法是配置參數好比用戶名和密碼等配置錯誤。
第二說法是在/etc/ssh/sshd_config文件夾的一個配置項PasswordAuthentication 默認爲no,「PasswordAuthentication」設置是否容許口令驗證,把它改成yes,重啓服務就OK了。
若是您在這一步就已經解決好Auth fail異常的話,祝賀您,您太幸運了!
-----------------------------------------
-----------------------------------------
-----------------------------------------
當我把以上方法都試了之後,仍是報Auth fail異常,這時要看一下SFTP鏈接代碼了,在鏈接SFTP代碼中加上下面代碼,另外再實現一個日誌類。
/**
* SFTP工具類
*/
public class SFTPUtil {
/**
* 鏈接SFTP服務器
* @throws JSchException
* @throws FileSystemException
*/
private void connectServer() throws JSchException, FileSystemException {
if (this.channel != null) {
disconnect();
}
com.jcraft.jsch.Logger logger = new SettleLogger();
JSch.setLogger(logger);
FileSystemOptions fso = new FileSystemOptions();
SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(fso, "no");
this.session = SftpClientFactory.createConnection(this.ftpHost,
this.ftpPort, this.userName.toCharArray(),
this.password.toCharArray(), fso);
Channel _channel = this.session.openChannel("sftp");
_channel.connect();
this.channel = ((ChannelSftp) _channel);
}
}
實現com.jcraft.jsch.Logger日誌,這個日誌是jsch自己的日誌工具類,和org.apache.log4j.Logger的日誌類不同。
/**
* 日誌工具類
*/
public class SettleLogger implements com.jcraft.jsch.Logger {
public boolean isEnabled(int level) {
return true;
}
public void log(int level, String msg) {
System.out.println(msg);
}
}
這樣程序的問題就解決了。。。java