//文件免下载
String user = LpayConfig.L_PAY_USERNAME;
String password = LpayConfig.L_PAY_PASSWORD;
String host = LpayConfig.L_PAY_IP;
int port = Integer.parseInt(LpayConfig.L_PAY_PORT);
String filePath = LpayConfig.L_PAY_FILE_PATH+"/"+"20230529";
String fileName = "1001_20230529.txt";
String remoteFile = filePath+"/"+fileName;
List<String> list = new ArrayList<>();
try {
JSch jsch = new JSch();
Session session = jsch.getSession(user, host, port);
session.setPassword(password);
/**
* session.setConfig("StrictHostKeyChecking", "no")是JSch库中的一个配置选项,作用是关闭对SSH服务器主机密钥的检查。
* 当SSH客户端第一次连接到SSH服务器时,会检查SSH服务器的主机密钥是否存在于客户端的known_hosts文件中。如果不存在,
* 则会提示用户确认是否信任该服务器的主机密钥。如果您不想手动确认,可以使用session.setConfig("StrictHostKeyChecking", "no")来关闭检查。
* 需要注意的是,关闭主机密钥检查可能会使您的SSH连接变得不安全,因为它允许未知的SSH服务器连接到您的客户端。
* 如果您要在生产环境中使用JSch库,建议使用session.setConfig("StrictHostKeyChecking", "yes")来启用主机密钥检查,
* 以确保SSH连接的安全性。
*/
session.setConfig("StrictHostKeyChecking", "no");
session.connect();
Channel channel = session.openChannel("sftp");
channel.connect();
ChannelSftp sftpChannel = (ChannelSftp) channel;
InputStream inputStream = sftpChannel.get(remoteFile);
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = reader.readLine()) != null) {
// 在这里处理每行数据
list.add(line+"\r\n");
}
reader.close();
sftpChannel.exit();
session.disconnect();
} catch (JSchException e) {
log.error("文件读取失败,请检查文件是否已经存在!");
e.printStackTrace();
} catch (SftpException e) {
log.error("sftp获取文件失败! " + e.getMessage());
throw new BizException("sftp获取对账文件失败! " + e.getMessage());
} catch (IOException e) {
log.error("文件读取失败,请检查文件是否已经存在!");
e.printStackTrace();
}
if (list.size() <= 0) {
log.info("获取sftp文件数据为空!");
}
JSch 流读取SFTP上的txt文件
猜你喜欢
转载自blog.csdn.net/weixin_52255395/article/details/131451446
今日推荐
周排行