问题
Am trying to pull files from an SFTP server using JSCH. I am getting the files without any problem from local host, but from the cloud server(SAP Cloud Platform), its giving this Connection Is Closed By Foreign Host exception from JSCH. I am getting the system proxy and port and setting the proxy with the request. Had seen the question asked several times, but none of them is solving my problem. The code snippet :
String SFTPHOST = "hostname";
int SFTPPORT = 22;
String SFTPUSER = "username";
String SFTPPASS = "password";
String SFTPWORKINGDIR = "directory";
Session session = null;
Channel channel = null;
ChannelSftp channelSftp = null;
try{
String proxyHost = System.getProperty("https.proxyHost");
String proxyPort = System.getProperty("https.proxyPort");
JSch jsch = new JSch();
session = jsch.getSession(SFTPUSER,SFTPHOST,SFTPPORT);
session.setPassword(SFTPPASS);
if(proxyHost != null && proxyPort != null) {
ProxyHTTP proxy = new ProxyHTTP(proxyHost, Integer.parseInt(proxyPort));
session.setProxy(proxy);
}
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
channel = session.openChannel("sftp");
channel.connect();
channelSftp = (ChannelSftp)channel;
channelSftp.cd(SFTPWORKINGDIR);
Vector filelist = channelSftp.ls(SFTPWORKINGDIR);
for(int i=0; i<filelist.size();i++){
LsEntry entry = (LsEntry) filelist.get(i);
response.getWriter().append(entry.getFilename());
}
}catch(Exception ex){
ex.printStackTrace();
}
来源:https://stackoverflow.com/questions/45147240/jschexception-connection-is-closed-by-foreign-host