struts下网站繁体的转换方法

以下内容均是我收集于互联网

 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配置

 

     < filter >
        
< filter-name > GbAndBig5Filter </ filter-name >
        
< filter-class > zeal.util.GbAndBig5Filter </ filter-class >
    
</ filter >
    
< filter-mapping >
        
< filter-name > GbAndBig5Filter </ filter-name >
        
< url-pattern > *.shtml </ url-pattern >
    
</ filter-mapping >
    
< filter-mapping >
        
< filter-name > GbAndBig5Filter </ filter-name >
        
< url-pattern > *.jsp </ url-pattern >
    
</ filter-mapping >
    
< filter-mapping >
        
< filter-name > GbAndBig5Filter </ filter-name >
        
< url-pattern > *.html </ url-pattern >
    
</ filter-mapping >
    
< filter-mapping >
        
< filter-name > GbAndBig5Filter </ filter-name >
        
< url-pattern > *.htm </ url-pattern >
    
</ filter-mapping >

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();
    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值