万能编码解码器
编程技术  /  houtizong 发布于 3年前   62
/**
* $Id: EncodingFilter.java,v 1.3 2008/08/20 20:02:05 cvs Exp $
*/
package com.cy.tiles.struts;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
public class EncodingFilter implements Filter
{
protected String encoding = null;
class Request extends HttpServletRequestWrapper
{
public Request(HttpServletRequest request) {
super(request);
}
/**
* 转换由表单读取的数据的内码.
* 从 ISO 字符转到 GBK.
*/
public String toChi(String input) {
try {
// System.out.println(input);
byte[] bytes = input.getBytes("ISO8859-1");
return new String(bytes, "UTF-8");
}
catch (Exception ex) {
}
return null;
}
/**
* Return the HttpServletRequest holded by this object.
*/
private HttpServletRequest getHttpServletRequest()
{
return (HttpServletRequest)super.getRequest();
}
/**
* 读取参数 -- 修正了中文问题.
*/
public String getParameter(String name)
{
return toChi(getHttpServletRequest().getParameter(name));
}
/**
* 读取参数列表 - 修正了中文问题.
*/
public String[] getParameterValues(String name)
{
System.out.println(":");
String values[] = getHttpServletRequest().getParameterValues(name);
if(values!=null)
values= values.clone();
if (values != null) {
for (int i = 0; i < values.length; i++) {
values[i] = toChi(values[i]);
}
}
return values==null?null:values;
}
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException
{
HttpServletRequest httpreq = (HttpServletRequest)request;
if(httpreq.getMethod()=="POST") {
request.setCharacterEncoding(encoding);
} else {
request = new Request(httpreq);
}
chain.doFilter(request,response);
}
public void init(FilterConfig filterConfig) throws ServletException
{
this.encoding = filterConfig.getInitParameter("encoding");
if (null == encoding){
throw new IllegalArgumentException("Please add encoding parameter");
}
}
public void destroy() {
}
}
<filter>
<filter-name>login</filter-name>
<filter-class>cn.mldn.lxh.filter.LoginFilter</filter-class>
<init-param>
<param-name>par</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>login</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!
技术博客集 - 网站简介:
前后端技术:
后端基于Hyperf2.1框架开发,前端使用Bootstrap可视化布局系统生成
网站主要作用:
1.编程技术分享及讨论交流,内置聊天系统;
2.测试交流框架问题,比如:Hyperf、Laravel、TP、beego;
3.本站数据是基于大数据采集等爬虫技术为基础助力分享知识,如有侵权请发邮件到站长邮箱,站长会尽快处理;
4.站长邮箱:[email protected];
文章归档
文章标签
友情链接