我正在尝试从SecureFTP客户端读取几个文件,以便运行每日报告任务:
两个文件的文件内容(
.bck
文件格式)在每一行中都是这样的
|0000000 1|FirstName|LastName|Active |0001|00000000|
|0000000 2|FirstName|LastName|Incctive|0002|0000000 1|
|0000000 3|FirstName|LastName|Active |0003|00020002|
到目前为止,我已经成功连接,能够读取文件名,但无法读取文件内容:
import net.schmizz.sshj.SSHClient;
import net.schmizz.sshj.sftp.SFTPClient;
import net.schmizz.sshj.transport.verification.PromiscuousVerifier;
import net.schmizz.sshj.sftp.RemoteFile.RemoteFileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.io.IOUtils;
SSHClient ssh = new SSHClient();
ssh.addHostKeyVerifier(new PromiscuousVerifier());
try {
ssh.connect("ServerName");
ssh.authPassword("username", "password");
SFTPClient sftp = ssh.newSFTPClient();
List fileContentsList = new ArrayList();
try {
List files = sftp.ls("/");
LocalDate yesterday = LocalDate.now().minusDays(1);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
boolean usFileFound = false;
boolean caFileFound = false;
for (file : files) {
String fileName = file.getName();
if (!file.isDirectory()) {
if (fileName.contains("SAPHR_POS_CAEEMD_") && fileName.contains(yesterday.format(formatter)) && !caFileFound) {
RemoteFile remoteFile = sftp.open(fileName); // Obtain the RemoteFile handle
InputStream inputStream = new RemoteFileInputStream(remoteFile); // Get the input stream
//List lines = Arrays.asList(fileContents.split("\n"));
String fileContents = IOUtils.toString(inputStream, "UTF-8"); // Read contents as a string
fileContentsList.add(fileContents); // Add the contents as string to the list
caFileFound = true;
remoteFile.close();
} else if (fileName.contains("SAPHR_POS_USEEMD_") && fileName.contains(yesterday.format(formatter)) && !usFileFound) {
RemoteFile remoteFile = sftp.open(fileName);
InputStream inputStream = new RemoteFileInputStream(remoteFile);
String fileContents = IOUtils.toString(inputStream, "UTF-8");
fileContentsList.add(fileContents);
usFileFound = true;
remoteFile.close();
}
if (caFileFound && usFileFound) {
break; // Stop after both files found
}
}
}
} finally {
sftp.close();
}
String result = String.join("\n", fileContentsList);
return result;
} finally {
try {
ssh.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
当前代码的输出:
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE List PUBLIC "sailpoint.dtd" "sailpoint.dtd">
<List>
<String>SAPHR_POS_CAEEMD_20230722.txt1690068889231.bck</String>
<String>SAPHR_POS_USEEMD_20230722.txt1690068894774.bck</String>
</List>
尝试执行上面代码中的注释行时收到错误:
Exception running rule: BeanShell script error: bsh.EvalError: Sourced file: inline evaluation of:
Author: user007 Date: 16-07-2023 Details: ********* . . . '' : Typed variable declaration : Error in method invocation: Method getInputStream(java.lang.String) not found in class'net.schmizz.sshj.sftp.SFTPClient' :
at Line: 48 : in file: inline evaluation of:
`` /*********** Author: user007 Date: 16-07-2023 Details: ********* . . . '' : sftp .getInputStream ( fileName ) BSF info: usersReport
at line: 0 column: columnNo