JSPを使用して動的ページを生成しています。この動的に生成された完全なページをアーカイブとしてファイルに保存したいと思います。
JSPでは、すべてがPrintWriter out = response.getWriter();
ページの終わりで、クライアントに応答を送信する前に、このページをファイルまたはバッファに保存して、後で処理できるようにします。
コンテンツを保存Printwriter
したり、に変換したりするにはどうすればよいString
ですか?
JSPを使用して動的ページを生成しています。この動的に生成された完全なページをアーカイブとしてファイルに保存したいと思います。
JSPでは、すべてがPrintWriter out = response.getWriter();
ページの終わりで、クライアントに応答を送信する前に、このページをファイルまたはバッファに保存して、後で処理できるようにします。
コンテンツを保存Printwriter
したり、に変換したりするにはどうすればよいString
ですか?
の出力から文字列を取得するには、コンストラクターを介してaをaにPrintWriter
渡すことができます。StringWriter
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);
}
StringWriter
代わりに使ってみませんか?これで必要なものが提供できるはずだと思います。
したがって、たとえば:
StringWriter strOut = new StringWriter();
...
String output = strOut.toString();
System.out.println(output);
それは、PrintWriterがどのように構築されて使用されるかに依存します。
PrintWriterが最初に構築され、それを書き込むコードに渡される場合は、PrintWriterをデリゲートとして受け取り、呼び出しをデリゲートに転送するWriterのサブクラスを作成できるデコレータパターンを使用できますが、また、アーカイブできるコンテンツのコピーも保持しています。
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();
}
}
PrintWriter
オブジェクトだけでは取得できません。データをフラッシュし、それ自体の中にコンテンツを保持しません。これは、文字列全体を取得するために確認する必要のあるオブジェクトではありません。
私が考える最善の方法は、StringBufferなどの他のオブジェクトで応答を準備し、そのコンテンツを応答にプッシュし、その変数に格納されているコンテンツをファイルに保存した後です。
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();
}
}
これは私を助けました:XML文字列としてSOAP可能なオブジェクトを取得するため。
JAXBContext jc = JAXBContext.newInstance(o.getClass());
Marshaller m = jc.createMarshaller();
StringWriter writer = new StringWriter();
m.marshal( o, new PrintWriter(writer) );
return writer.toString();