java 将FTP服务器上的附件下载到本地
private boolean downloadFile(String userName, String hostName, String password, String remoteDownloadDirectory, String flowId, String localDirectory, String downloadFileName) {
log.info("=========FTP文件下载===========");
boolean flag=false;
OutputStream out = null;
try {
Session session = null;
JSch jSch = new JSch();
session = jSch.getSession(userName, hostName);
session.setPassword(password);
Properties sshConfig = new Properties();
sshConfig.put("StrictHostKeyChecking", "no");
session.setConfig(sshConfig);
session.connect();
ChannelSftp channelSftp = (ChannelSftp) session.openChannel("sftp");
channelSftp.connect();
channelSftp.get(remoteDownloadDirectory, localDirectory);
if (null != session) {
channelSftp.disconnect();
session.disconnect();
log.info("ftp 服务器连接下载成功....");
} else {
log.info("ftp 服务器连接下载失败....");
}
flag=true;
} catch (Exception e) {
log.info("=======报错信息========"+e.getMessage());
flag=false;
} finally {
try {
if (out != null) {
out.flush();
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return flag;
}