22

我正在使用 JSP 生成动态页面,我想将这个动态生成的完整页面保存在文件中作为存档。

在 JSP 中,所有内容都写入PrintWriter out = response.getWriter();

在页面末尾,在向客户端发送响应之前,我想将此页面保存在文件中或缓冲区中作为字符串供以后处理。

如何保存Printwriter内容或转换为String?

4

7 回答 7

48

要从 a 的输出中获取字符串PrintWriter,可以通过构造函数将 a 传递StringWriter给 a :PrintWriter

@Test
public void writerTest(){
    StringWriter out    = new StringWriter();
    PrintWriter  writer = new PrintWriter(out);

    // use writer, e.g.:
    writer.print("ABC");
    writer.print("DEF");

    writer.flush(); // flush is really optional here, as Writer calls the empty StringWriter.flush
    String result = out.toString();

    assertEquals("ABCDEF", result);
}
于 2017-03-01T15:40:29.173 回答
11

为什么不StringWriter改用?我认为这应该能够提供您所需要的。

例如:

StringWriter strOut = new StringWriter();
...
String output = strOut.toString();
System.out.println(output);
于 2013-03-20T04:01:16.710 回答
7

这将取决于: PrintWriter 的构造和使用方式。

如果首先构造 PrintWriter 然后将其传递给写入它的代码,则可以使用装饰器模式,该模式允许您创建 Writer 的子类,它将 PrintWriter 作为委托,并将调用转发给委托,但是还保留内容的副本,然后您可以将其存档。

public class DecoratedWriter extends Writer
{
   private final Writer delegate;

   private final StringWriter archive = new StringWriter();

   //pass in the original PrintWriter here
   public DecoratedWriter( Writer delegate )
   {
      this.delegate = delegate;
   }

   public String getForArchive()
   { 
      return this.archive.toString();
   } 

   public void write( char[] cbuf, int off, int len ) throws IOException
   {
      this.delegate.write( cbuf, off, len );
      this.archive.write( cbuf, off, len );
   }

   public void flush() throws IOException
   {
      this.delegate.flush();
      this.archive.flush();

   } 

   public void close() throws IOException
   {
      this.delegate.close();
      this.archive.close();
   }
}
于 2012-02-10T11:25:23.880 回答
1

你不能只用你的PrintWriter对象得到它。它刷新数据,并且本身不包含任何内容。这不是您应该查看获取整个字符串的对象,

于 2012-02-10T11:12:24.287 回答
0

我认为最好的方法是在 StringBuffer 等其他对象中准备您的响应,并将其内容推送到响应中,然后将存储在该变量中的内容保存到文件中。

于 2012-02-10T12:26:41.150 回答
0

与 cdc 所做的类似 - 您可以扩展PrintWriter,然后创建和传递这个新类的实例。

调用getArchive()以获取通过编写器传递的数据的副本。

public class ArchiveWriter extends PrintWriter {
    private StringBuilder data = new StringBuilder();

    public ArchiveWriter(Writer out) {
        super(out);
    }

    public ArchiveWriter(Writer out, boolean autoFlush) {
        super(out, autoFlush);
    }

    public ArchiveWriter(OutputStream out) {
        super(out);
    }

    public ArchiveWriter(OutputStream out, boolean autoFlush) {
        super(out, autoFlush);
    }

    public ArchiveWriter(String fileName) throws FileNotFoundException {
        super(fileName);
    }

    public ArchiveWriter(String fileName, String csn) throws FileNotFoundException, UnsupportedEncodingException {
        super(fileName, csn);
    }

    public ArchiveWriter(File file) throws FileNotFoundException {
        super(file);
    }

    public ArchiveWriter(File file, String csn) throws FileNotFoundException, UnsupportedEncodingException {
        super(file, csn);
    }

    @Override
    public void write(char[] cbuf, int off, int len) {
        super.write(cbuf, off,len);
        data.append(cbuf, off, len);
    }

    @Override
    public void write(String s, int off, int len) {
        super.write(s, off,len);
        data.append(s, off, len);
    }

    public String getArchive() {
        return data.toString();
    }
}
于 2016-08-11T16:52:16.483 回答
-1

这帮助了我:获得一个支持 SOAP 的对象作为 XML 字符串。

JAXBContext jc = JAXBContext.newInstance(o.getClass());
Marshaller m = jc.createMarshaller();
StringWriter writer = new StringWriter();
m.marshal( o, new PrintWriter(writer) );
return writer.toString();
于 2013-08-07T20:34:38.253 回答