我这样做:
@Overridepublic ServletOutputStream getOutputStream() throws IOException { // this is called onLY in WebLogic // created a custom outputstream that wraps your charArray return new CustomOutputStream(this.charArray);}// custom outputstream to wrap charArray writerclass CustomOutputStream extends ServletOutputStream { private WriterOutputStream out; public CustomOutputStream(CharArrayWriter writer) { // WriterOutputStream has a constructor without charset but it's deprecated, so change the UTF-8 charset to the one you use, if needed this.out = new WriterOutputStream(writer, "UTF-8"); } @Override public boolean isReady() { return true; } @Override public void setWriteListener(WriteListener writeListener) { } @Override public void write(int b) throws IOException { this.out.write(b); this.out.flush(); // it doesn't work without flushing }}
我使用的
WriterOutputStream是Apache Commons-io,因此必须在pom.xml中添加它:
<dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.5</version></dependency>
我不知道您的jsp文件中包含什么,但是我已经用一个简单的文件进行了测试,我相信它可以工作。我的jsp文件:
<b>Hello world</b><p>testing</p><ul>test<li>item</li><li>item2</li></ul>
输出(在浏览器中访问servlet时):
{"results":"<b>Hello world</b>nn<p>testing</p>nn<ul>testn<li>item</li>n<li>item2</li>n</ul>"}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)