【问题标题】:Copy System.out to a File using Jsch and Java使用 Jsch 和 Java 将 System.out 复制到文件
【发布时间】:2016-01-20 18:54:28
【问题描述】:

我正在使用 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();

【问题讨论】:

    标签: java shell outputstream jsch


    【解决方案1】:

    您可以创建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);
    

    【讨论】:

    • 不错的方法,您能否在您的代码中添加一些 cmets,因为我以前从未这样做过。另外,你能告诉我如何访问你的子类吗?你能把你的答案和我的代码结合起来吗?我想将输出写入 outputFilePath 和 System.out
    • 添加了示例用法。添加 cmets 没有多大意义。每个方法都与 FilterOutputStream 做同样的事情,但该操作适用于所有 OutputStreams,而不是像 FilterOutputStream 通常做的那样。
    • 再次感谢您的及时回复。现在我得到非静态变量,这不能从静态内容中引用
    • 我已将 MultiplexOutputStream 更改为静态并且它有效。谢谢!!
    猜你喜欢
    • 1970-01-01
    • 2011-06-17
    • 2015-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多