尝试使用JSCH从SFTP服务器提取文件。我从本地主机获取文件时没有任何问题,但是从云服务器(SAP Cloud Platform),它从JSCH给出了这个连接被外部主机异常关闭。我正在获取系统代理和端口并使用请求设置代理。 曾经多次看过这个问题,但他们都没有解决我的问题。代码段:
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();
}