英文:
How to read files from SFTP client without JSch
问题
以下是您要求的代码部分的翻译:
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<String> fileContentsList = new ArrayList<>();
try {
List<RemoteResourceInfo> files = sftp.ls("/");
LocalDate yesterday = LocalDate.now().minusDays(1);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
boolean usFileFound = false;
boolean caFileFound = false;
for (RemoteResourceInfo 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); // 获取远程文件的句柄
InputStream inputStream = new RemoteFileInputStream(remoteFile); // 获取输入流
String fileContents = IOUtils.toString(inputStream, "UTF-8"); // 以字符串形式读取内容
fileContentsList.add(fileContents); // 将内容作为字符串添加到列表中
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; // 找到两个文件后停止
}
}
}
} finally {
sftp.close();
}
String result = String.join("\n", fileContentsList);
return result;
} finally {
try {
ssh.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
请注意,我已经对代码中的HTML实体进行了翻译。如果您有任何进一步的问题或需要其他帮助,请随时告诉我。
英文:
I'm trying to read a couple of files from SecureFTP client for run a daily report task:
The file contents from both the files(.bck file format) looks like this in each line
|00000001|FirstName|LastName|Active|0001|00000000|
|00000002|FirstName|LastName|Incctive|0002|00000001|
|00000003|FirstName|LastName|Active|0003|00020002|
So far, I have successfully made the connection and able to read the file names but unable to read the file contents:
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();
}
}
The output from current code:
<?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>
The error received while trying to perform the commented lines in above code:
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
答案1
得分: 1
我认为问题在于您错误地使用了API。
如果您查看SFTPClient
的示例,它们不使用名为getretrieveFileStream
的方法。实际上,API中没有一个方法的名称远程类似于那个。调用不存在的方法永远不会起作用。
我建议您自己查看示例,看看它们是否符合您的要求。并查看API的javadoc文档。例如:
我怀疑提取文件内容的模式可能类似于以下步骤:
- 获取一个
SFTPClient
。 - 在客户端上调用
open
以获取要读取的文件的RemoteFile
句柄。 - 使用该句柄作为外部类上下文,调用
RemoteFileInputStream
的无参数构造函数以获取输入流。 - 以正常方式从输入流中读取
- 关闭流和句柄
英文:
I think that the problem is that you are using the APIs incorrectly.
If you look at the examples for SFTPClient
, they don't use a method called getretrieveFileStream
. In fact, there isn't a method in the API whose name remotely resembles that. Calling non-existent methods is never going to work.
I recommend that you look at the examples for yourself and see if they do what you want. And look at the javadocs for the APIs. For example:
I suspect the pattern for extracting the contents of a file will be something like this:
- Obtain an
SFTPClient
. - Call
open
on the client to obtain aRemoteFile
handle for the file you want to read. - Using that handle as the outer class context, invoke the
RemoteFileInputStream
no-args constructor to get an input stream. - Read from the input stream in the normal way
- Close the stream and the handle
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论