java 过滤器阻断输出流,如何使用Java中的过滤器基于HTML文档输出二进制文档

This may be a little confusing but I having some trouble. My goal is to take an input HTML document and then process that document and use the HTML data to output a image document. For example, a user will request a URL, with some action=png in the querystring and then the filter will get invoked for the URL and output image document.

I have tried everything but in my environment (Websphere), I am only able to output of one type. If the input type is text/html then I can only output a text document, I can't seem to output a binary document. Why? Because I get an illegal state exception each time.

[1/29/09 17:59:57:576 EST] 00000020 SystemErr R java.lang.IllegalStateException: SRVE0209E: Writer already obtained

[1/29/09 17:59:57:576 EST] 00000020 SystemErr R at com.ibm.ws.webcontainer.srt.SRTServletResponse.getOutputStream(SRTServletResponse.java:505)

I am using pseudo code not to reveal all of my code:

TestFilter

com.util.TestFilter

TestFilter

/index_test2.html

The Wrapper Class code is essentially this:

public class ContentCaptureServletResponse extends HttpServletResponseWrapper {

private ByteArrayOutputStream contentBuffer;

private PrintWriter writer;

public PrintWriter getWriter() throws IOException {

if (writer == null) {

contentBuffer = new ByteArrayOutputStream();

writer = new PrintWriter(contentBuffer);

}

return writer;

}

public String getContent(){

try {

writer = getWriter();

} catch (IOException e) {

e.printStackTrace();

}

writer.flush();

String xhtmlContent = new String(contentBuffer.toByteArray());

System.out.println(xhtmlContent);

return xhtmlContent;

}

}

And the filter code is this:

public void doFilter(ServletRequest req, ServletResponse resp, FilterChain filterChain) throws IOException, ServletException {

HttpServletRequest request = (HttpServletRequest) req;

HttpServletResponse response = (HttpServletResponse) resp;

final String renderType = request.getParameter("R");

final String renderClassName = request.getParameter("C");

if ((renderType != null) && (renderClassName != null)) {

try {

this.setFilterChain(filterChain);

response.setContentType("image/png");

PrintWriter out = response.getWriter();

// I call getWriter but not BOTH!

//response.getOutputStream();

response.getWriter();

// Build up the HTML document into a string.

CaptureResponse capContent = new CaptureResponse(response);

this.mainFilterChain.doFilter(req, );

String myString = capHtmlContent.getContent();

// What I really want to do here is output an output stream

// so I can write a binary image

processStr(myString);

response.getOutputStream();

response.write(binaryimage)

} catch (Exception e) {

e.printStackTrace();

}

// No data will be output to the user.

} else {

filterChain.doFilter(request, response);

} // End of if-else

} // End of the method.

The code works if I want to take some input html text document. I am assuming because of the open printwriter stream. But I am having trouble going to a different format. Basically, because I can't call response.getOutputStream()

解决方案

The issue does appear to be that you are opening the response Writer prior to wrapping the response.

It looks like you should be able to do:

this.setFilterChain(filterChain);

CaptureContent capContent = new CaptureResponse(response);

doFilter()

process();

response.setContentType("image/png");

response.getOutputStream().write(imagedata);

You definitely cannot safely open both the Writer and the OutputStream

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值