JSchException: connection is closed by foreign host

♀尐吖头ヾ 提交于 2019-12-08 12:18:33

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!