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

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

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(&quot;ServerName&quot;);
ssh.authPassword(&quot;username&quot;, &quot;password&quot;);
SFTPClient sftp = ssh.newSFTPClient();
List fileContentsList = new ArrayList();
try {
List files = sftp.ls(&quot;/&quot;);
LocalDate yesterday = LocalDate.now().minusDays(1);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(&quot;yyyyMMdd&quot;);
boolean usFileFound = false;
boolean caFileFound = false;
for (file : files) {
String fileName = file.getName();
if (!file.isDirectory()) {
if (fileName.contains(&quot;SAPHR_POS_CAEEMD_&quot;) &amp;&amp; fileName.contains(yesterday.format(formatter)) &amp;&amp; !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(&quot;\n&quot;));
String fileContents = IOUtils.toString(inputStream, &quot;UTF-8&quot;); // Read contents as a string
fileContentsList.add(fileContents); // Add the contents as string to the list
caFileFound = true;
remoteFile.close();
} else if (fileName.contains(&quot;SAPHR_POS_USEEMD_&quot;) &amp;&amp; fileName.contains(yesterday.format(formatter)) &amp;&amp; !usFileFound) {
RemoteFile remoteFile = sftp.open(fileName); 
InputStream inputStream = new RemoteFileInputStream(remoteFile);
String fileContents = IOUtils.toString(inputStream, &quot;UTF-8&quot;);
fileContentsList.add(fileContents);
usFileFound = true;
remoteFile.close();
}
if (caFileFound &amp;&amp; usFileFound) {
break; // Stop after both files found
}
}
}
} finally {
sftp.close();
}
String result = String.join(&quot;\n&quot;, fileContentsList);
return result;
} finally {
try {
ssh.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}

The output from current code:

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

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: ********* . . . &#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; :
at Line: 48 : in file: inline evaluation of:
``   /*********** Author: user007  Date: 16-07-2023	 Details: ********* . . . &#39;&#39; : sftp .getInputStream ( fileName )   BSF info: usersReport
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:

确定