擷取jsp輸出內容來進行簡繁轉換

來源:互聯網
上載者:User

1、繁體字轉換工具

用來處理GB2312/BIG5碼字元互相轉換的類.可到網上搜尋

2、filter攔截方法

public class FileCaptureResponseWrapper extends HttpServletResponseWrapper ...{
public static final int OT_NONE = 0, OT_WRITER = 1, OT_STREAM = 2;

private int outputType = OT_NONE;

private ServletOutputStream output = null;
private PrintWriter writer = null;
private ByteArrayOutputStream buffer = null;

public FileCaptureResponseWrapper(HttpServletResponse resp) throws IOException ...{
super(resp);
buffer = new ByteArrayOutputStream();
}

public PrintWriter getWriter() throws IOException ...{
if(outputType==OT_STREAM)
throw new IllegalStateException();
else if(outputType==OT_WRITER)
return writer;
else ...{
outputType = OT_WRITER;
writer = new PrintWriter(new OutputStreamWriter(buffer, getCharacterEncoding()));
return writer;
}
}

public ServletOutputStream getOutputStream() throws IOException ...{
if(outputType==OT_WRITER)
throw new IllegalStateException();
else if(outputType==OT_STREAM)
return output;
else ...{
outputType = OT_STREAM;
output = new WrappedOutputStream(buffer);
return output;
}
}

public void flushBuffer() throws IOException ...{
if(outputType==OT_WRITER)
writer.flush();
if(outputType==OT_STREAM)
output.flush();
}

public void reset() ...{
outputType = OT_NONE;
buffer.reset();
}

public byte[] toByteArray() throws IOException ...{
flushBuffer();
return buffer.toByteArray();
}

}

class WrappedOutputStream extends ServletOutputStream ...{

private ByteArrayOutputStream buffer;

public WrappedOutputStream(ByteArrayOutputStream buffer) ...{
this.buffer = buffer;
}

public void write(int b) throws IOException ...{
buffer.write(b);
}

public byte[] toByteArray() ...{
return buffer.toByteArray();
}

}

3、web.xml配置


GbAndBig5Filter
zeal.util.GbAndBig5Filter


GbAndBig5Filter
*.shtml


GbAndBig5Filter
*.jsp


GbAndBig5Filter
*.html


GbAndBig5Filter
*.htm

4、過濾器

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
ServletException ...{

HttpServletRequest httpRequest = (HttpServletRequest) request;
HttpServletResponse httpResponse = (HttpServletResponse) response;
FileCaptureResponseWrapper wrapperResponse = new FileCaptureResponseWrapper(httpResponse);
chain.doFilter(httpRequest, wrapperResponse);
byte[] bytes=wrapperResponse.toByteArray();
String s="";
try ...{
s = new String(GB2Big5.getInstance().gb2big5(bytes,"UTF-8"), "BIG5");
} catch (Exception e) ...{
e.printStackTrace();
}


// response.setContentType("text/html;charset=UTF-8");
PrintWriter pr = response.getWriter();
pr.print(s);
pr.flush();
pr.close();
}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.