java 如何将文件内容从一个服务器传输到另一个服务器[已关闭]

wyyhbhjk  于 2023-04-10  发布在  Java
关注(0)|答案(2)|浏览(183)

已关闭,此问题需要更focused,目前不接受回答。
**要改进此问题吗?**更新问题,使其仅关注editing this post的一个问题。

昨天关门了。
Improve this question
文件类型- txt,csv,pdf,docx,xlsx,xml我已经使用sftp ref -connection code连接到服务器我已经使用库在我的本地机器上编写了一个代码,它将从上述文件类型中读取内容。我建立连接的服务器有文件,我应该如何从文件中读取内容。条件是我不能将文件从服务器复制到我的本地机器。
我能够使用BufferReader读取文本文件的内容并将内容传输到我的本地机器。如何为所有文件类型实现这一点?

xiozqbni

xiozqbni1#

您可以使用InputStream从服务器获取文档到本地计算机,并使用InputStream从文件中提取内容。

tquggr8v

tquggr8v2#

试试这个

Session session = null;
ChannelExec channel = null;
try {
  session = new JSch().getSession(username, host, port);
  session.setPassword(password);
  session.setConfig("StrictHostKeyChecking", "no");
  session.connect();

  channel = (ChannelExec) session.openChannel("exec");
  channel.setCommand("cat /home/user/a-file.txt");
  ByteArrayOutputStream responseStream = new ByteArrayOutputStream();
  channel.setOutputStream(responseStream);
  channel.connect();

  while (channel.isConnected()) {
    Thread.sleep(100);
  }
  
  //THIS IS FILE CONTENT
  byte[] content = responseStream.toByteArray();
} catch (Exception e) {
  throw new RuntimeException(e);
} finally {
  if (session != null) {
    session.disconnect();
  }
  if (channel != null) {
    channel.disconnect();
  }
}

相关问题