如何在不使用JSch的情况下从SFTP客户端读取文件

huangapple go评论109阅读模式
英文:

How to read files from SFTP client without JSch

问题

以下是您要求的代码部分的翻译:

  1. import net.schmizz.sshj.SSHClient;
  2. import net.schmizz.sshj.sftp.SFTPClient;
  3. import net.schmizz.sshj.transport.verification.PromiscuousVerifier;
  4. import net.schmizz.sshj.sftp.RemoteFile.RemoteFileInputStream;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.time.LocalDate;
  8. import java.time.format.DateTimeFormatter;
  9. import java.util.ArrayList;
  10. import java.util.List;
  11. import org.apache.commons.io.IOUtils;
  12. SSHClient ssh = new SSHClient();
  13. ssh.addHostKeyVerifier(new PromiscuousVerifier());
  14. try {
  15. ssh.connect("ServerName");
  16. ssh.authPassword("username", "password");
  17. SFTPClient sftp = ssh.newSFTPClient();
  18. List<String> fileContentsList = new ArrayList<>();
  19. try {
  20. List<RemoteResourceInfo> files = sftp.ls("/");
  21. LocalDate yesterday = LocalDate.now().minusDays(1);
  22. DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
  23. boolean usFileFound = false;
  24. boolean caFileFound = false;
  25. for (RemoteResourceInfo file : files) {
  26. String fileName = file.getName();
  27. if (!file.isDirectory()) {
  28. if (fileName.contains("SAPHR_POS_CAEEMD_") && fileName.contains(yesterday.format(formatter)) && !caFileFound) {
  29. RemoteFile remoteFile = sftp.open(fileName); // 获取远程文件的句柄
  30. InputStream inputStream = new RemoteFileInputStream(remoteFile); // 获取输入流
  31. String fileContents = IOUtils.toString(inputStream, "UTF-8"); // 以字符串形式读取内容
  32. fileContentsList.add(fileContents); // 将内容作为字符串添加到列表中
  33. caFileFound = true;
  34. remoteFile.close();
  35. } else if (fileName.contains("SAPHR_POS_USEEMD_") && fileName.contains(yesterday.format(formatter)) && !usFileFound) {
  36. RemoteFile remoteFile = sftp.open(fileName);
  37. InputStream inputStream = new RemoteFileInputStream(remoteFile);
  38. String fileContents = IOUtils.toString(inputStream, "UTF-8");
  39. fileContentsList.add(fileContents);
  40. usFileFound = true;
  41. remoteFile.close();
  42. }
  43. if (caFileFound && usFileFound) {
  44. break; // 找到两个文件后停止
  45. }
  46. }
  47. }
  48. } finally {
  49. sftp.close();
  50. }
  51. String result = String.join("\n", fileContentsList);
  52. return result;
  53. } finally {
  54. try {
  55. ssh.disconnect();
  56. } catch (IOException e) {
  57. e.printStackTrace();
  58. }
  59. }

请注意,我已经对代码中的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:

  1. import net.schmizz.sshj.SSHClient;
  2. import net.schmizz.sshj.sftp.SFTPClient;
  3. import net.schmizz.sshj.transport.verification.PromiscuousVerifier;
  4. import net.schmizz.sshj.sftp.RemoteFile.RemoteFileInputStream;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.time.LocalDate;
  8. import java.time.format.DateTimeFormatter;
  9. import java.util.ArrayList;
  10. import java.util.List;
  11. import org.apache.commons.io.IOUtils;
  12. SSHClient ssh = new SSHClient();
  13. ssh.addHostKeyVerifier(new PromiscuousVerifier());
  14. try {
  15. ssh.connect(&quot;ServerName&quot;);
  16. ssh.authPassword(&quot;username&quot;, &quot;password&quot;);
  17. SFTPClient sftp = ssh.newSFTPClient();
  18. List fileContentsList = new ArrayList();
  19. try {
  20. List files = sftp.ls(&quot;/&quot;);
  21. LocalDate yesterday = LocalDate.now().minusDays(1);
  22. DateTimeFormatter formatter = DateTimeFormatter.ofPattern(&quot;yyyyMMdd&quot;);
  23. boolean usFileFound = false;
  24. boolean caFileFound = false;
  25. for (file : files) {
  26. String fileName = file.getName();
  27. if (!file.isDirectory()) {
  28. if (fileName.contains(&quot;SAPHR_POS_CAEEMD_&quot;) &amp;&amp; fileName.contains(yesterday.format(formatter)) &amp;&amp; !caFileFound) {
  29. RemoteFile remoteFile = sftp.open(fileName); // Obtain the RemoteFile handle
  30. InputStream inputStream = new RemoteFileInputStream(remoteFile); // Get the input stream
  31. //List lines = Arrays.asList(fileContents.split(&quot;\n&quot;));
  32. String fileContents = IOUtils.toString(inputStream, &quot;UTF-8&quot;); // Read contents as a string
  33. fileContentsList.add(fileContents); // Add the contents as string to the list
  34. caFileFound = true;
  35. remoteFile.close();
  36. } else if (fileName.contains(&quot;SAPHR_POS_USEEMD_&quot;) &amp;&amp; fileName.contains(yesterday.format(formatter)) &amp;&amp; !usFileFound) {
  37. RemoteFile remoteFile = sftp.open(fileName);
  38. InputStream inputStream = new RemoteFileInputStream(remoteFile);
  39. String fileContents = IOUtils.toString(inputStream, &quot;UTF-8&quot;);
  40. fileContentsList.add(fileContents);
  41. usFileFound = true;
  42. remoteFile.close();
  43. }
  44. if (caFileFound &amp;&amp; usFileFound) {
  45. break; // Stop after both files found
  46. }
  47. }
  48. }
  49. } finally {
  50. sftp.close();
  51. }
  52. String result = String.join(&quot;\n&quot;, fileContentsList);
  53. return result;
  54. } finally {
  55. try {
  56. ssh.disconnect();
  57. } catch (IOException e) {
  58. e.printStackTrace();
  59. }
  60. }

The output from current code:

  1. &lt;?xml version=&#39;1.0&#39; encoding=&#39;UTF-8&#39;?&gt;
  2. &lt;!DOCTYPE List PUBLIC &quot;sailpoint.dtd&quot; &quot;sailpoint.dtd&quot;&gt;
  3. &lt;List&gt;
  4. &lt;String&gt;SAPHR_POS_CAEEMD_20230722.txt1690068889231.bck&lt;/String&gt;
  5. &lt;String&gt;SAPHR_POS_USEEMD_20230722.txt1690068894774.bck&lt;/String&gt;
  6. &lt;/List&gt;

The error received while trying to perform the commented lines in above code:

  1. Exception running rule: BeanShell script error: bsh.EvalError: Sourced file: inline evaluation of:
  2. Author: user007 Date: 16-07-2023 Details: ********* . . . &#39;&#39; : Typed variable declaration : Error in method invocation: Method getInputStream(java.lang.String) not found in class&#39;net.schmizz.sshj.sftp.SFTPClient&#39; :
  3. at Line: 48 : in file: inline evaluation of:
  4. `` /*********** Author: user007 Date: 16-07-2023 Details: ********* . . . &#39;&#39; : sftp .getInputStream ( fileName ) BSF info: usersReport
  5. at line: 0 column: columnNo

答案1

得分: 1

我认为问题在于您错误地使用了API。

如果您查看SFTPClient示例,它们不使用名为getretrieveFileStream的方法。实际上,API中没有一个方法的名称远程类似于那个。调用不存在的方法永远不会起作用。

我建议您自己查看示例,看看它们是否符合您的要求。并查看API的javadoc文档。例如:

我怀疑提取文件内容的模式可能类似于以下步骤:

  1. 获取一个SFTPClient
  2. 在客户端上调用open以获取要读取的文件的RemoteFile句柄。
  3. 使用该句柄作为外部类上下文,调用RemoteFileInputStream的无参数构造函数以获取输入流。
  4. 以正常方式从输入流中读取
  5. 关闭流和句柄
英文:

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:

  1. Obtain an SFTPClient.
  2. Call open on the client to obtain a RemoteFile handle for the file you want to read.
  3. Using that handle as the outer class context, invoke the RemoteFileInputStream no-args constructor to get an input stream.
  4. Read from the input stream in the normal way
  5. Close the stream and the handle

huangapple
  • 本文由 发表于 2023年7月23日 14:05:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76746830.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定