java - 使用 Jsch 和 Java 将 System.out 复制到文件

标签 java shell outputstream jsch

我正在使用 JSCH 和 Shell 对主机运行多个命令。一切正常,但我的问题是如何获取 System.out 并将其保存到文件中。我希望复制而不是重定向。我可以做其中之一,但不能两者都做。

try (OutputStream logOutput = new BufferedOutputStream(new FileOutputStream(outputFilePath))) {
            try (InputStream login = new BufferedInputStream(new FileInputStream(outputFilePath))) {

     JSch jsch = new JSch();
     Session session = jsch.getSession(user, host, 22);
     session.setPassword(password);
     session.setConfig(getProperties());
     session.connect(10 * 1000);
     Channel channel = session.openChannel("shell");
     //channel.setOutputStream(System.out);// I want to activate it as well as the following command
    channel.setOutputStream(logOutPut, true);// I am writing it to file
     try (PipedInputStream commandSource = new PipedInputStream();
           OutputStream commandSink = new PipedOutputStream(commandSource)) {
           CommandSender sender = new CommandSender(commandSink);
           Thread sendThread = new Thread(sender);
           sendThread.start();

           channel.setInputStream(commandSource);
           channel.connect(15 * 1000);
           sendThread.join();
                  if (sender.exception != null) {
                         throw sender.exception;
                             }
                   }
                   channel.disconnect();
                   session.disconnect();

最佳答案

您可以创建 FilterOutputStream 的子类它将相同的字节写入多个输出流:

public class MultiplexOutputStream
extends FilterOutputStream {
    private final OutputStream[] streams;

    public MultiplexOutputStream(OutputStream stream,
                                 OutputStream... otherStreams) {
        super(stream);
        this.streams = otherStreams.clone();

        for (OutputStream otherStream : otherStreams) {
            Objects.requireNonNull(otherStream,
                "Null OutputStream not permitted");
        }
    }

    @Override
    public void write(int b)
    throws IOException {
        super.write(b);
        for (OutputStream stream : streams) {
            stream.write(b);
        }
    }

    @Override
    public void write(byte[] bytes)
    throws IOException {
        super.write(bytes);
        for (OutputStream stream : streams) {
            stream.write(bytes);
        }
    }

    @Override
    public void write(byte[] bytes,
                      int offset,
                      int length)
    throws IOException {
        super.write(bytes, offset, length);
        for (OutputStream stream : streams) {
            stream.write(bytes, offset, length);
        }
    }

    @Override
    public void flush()
    throws IOException {
        super.flush();
        for (OutputStream stream : streams) {
            stream.flush();
        }
    }

    @Override
    public void close()
    throws IOException {
        super.close();
        for (OutputStream stream : streams) {
            stream.close();
        }
    }
}

要在代码中使用它:

channel.setOutputStream(new MultiplexOutputStream(logOutput, System.out), true);

关于java - 使用 Jsch 和 Java 将 System.out 复制到文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34908166/

相关文章:

c++ - 间接需要指针操作数

java - 在 Apache Poi 中调用 trackAllColumnsForAutoSIizing 的内存影响是什么

LINUX - saltstack cmd.run 在变量中

bash - 使用 shell 脚本过滤目录列表

multithreading - 进程可以读取自己的 "standard out"流吗?

java - 为什么在java中以multipart/form-data发送文件时我们必须同时使用Writer和OutputStream?

java - mysql:查找具有作为 "string"的子字符串的字段的行

java - 从 Google Java API 数据存储迁移到 Mysql

Java - 随着时间的推移打开 hibernate session 的问题

macos - 在哪里放置 shell 脚本?