java中文乱码问题
来源:互联网 发布:python顶级黑客 编辑:程序博客网 时间:2024/04/29 22:46
对于中文乱码问题,有时真的困惑我们很久,在我的实践中,我总结了一下
前提条件是要你配制好统一的编码格式 (utf-8,gbk,iso-8859-1);
1,如果当你提交请求是post 请求时,如果发现有乱码,你可以改用get请求,这样也能避免乱码
2, 当你从页面上面传的过来的参数少时,你可以在后台一个一个的处理,比如:
把从页面上传过来的字符串进行乱码处理
String userName=req.getParameter("userName");
String name=new String (userName.getBytes("ISO-8859-1"),"UTF-8");
3 ,你可以自己做一个小小的过滤器
package com.demo.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;
public class FileFilter implements Filter{
private FilterConfig config;
public void destroy() {
// TODO 自动生成方法存根
}
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
// TODO 自动生成方法存根
System.out.println("进入到了过滤器");
req.setCharacterEncoding("UTF-8");
chain.doFilter(req,resp);
}
public void init(FilterConfig config) throws ServletException {
this.config=config;
// TODO 自动生成方法存根
}
}
4, 你可以用“万能编码过滤器”(我自己取的,哈哈)
package filt;
import java.util.*;
import java.io.IOException;
import javax.servlet.*;
import javax.servlet.http.*;
public class EncodingFilter implements Filter{
private FilterConfig filterConfig;
//Handle the passed-in FilterConfig
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
}
//Process the request/response pair
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain filterChain) {
String encoding=filterConfig.getInitParameter("encoding");
try {
System.out.println("通过了字符编码过滤器");
HttpServletRequest request1=(HttpServletRequest)request;
if(request1.getMethod().equalsIgnoreCase("post")){
System.out.println("通过了post请求过滤器");
request.setCharacterEncoding(encoding);
}else if(request1.getMethod().equalsIgnoreCase("get")){
System.out.println("通过了get请求过滤器");
Map map=request1.getParameterMap();
Set set=map.keySet();
Iterator it=set.iterator();
while(it.hasNext()){
String key=(String)it.next();
Object value=map.get(key);
if(value instanceof String[]){
String[] values=(String[])value;
for (int i = 0; i < values.length; i++) {
values[i]=new String(values[i].getBytes("ISO-8859-1"),"GBK");
}
}
}
}
filterChain.doFilter(request, response);
} catch (ServletException sx) {
filterConfig.getServletContext().log(sx.getMessage());
} catch (IOException iox) {
filterConfig.getServletContext().log(iox.getMessage());
}
}
//Clean up resources
public void destroy() {
}
}
你还要在web.xml里面配制一下
- java中文乱码问题
- java中文乱码问题
- JAVA中文乱码问题
- java 中文乱码问题
- java 中文乱码问题
- java 中文乱码问题
- java中文乱码问题
- java中文乱码问题
- java中文乱码问题
- java中文乱码问题
- java中文乱码问题
- java 中文乱码问题实验
- Java/JSP中文乱码问题
- Java/JSP中文乱码问题
- JAVA处理中文乱码问题
- JAVA中文显示乱码问题
- Java压缩中文乱码问题
- Java的中文乱码问题
- 加快C++的编译速度
- 效率优化总结
- 是否需要汉语编程语言
- ZJU2376 Ants - 碰头返回==相互穿过
- C++实现CD抓轨转WAV
- java中文乱码问题
- 需要避免的MYSQL客户机程序设计错误
- C#常用知识汇总问与答(不断更新中...)
- oracle DBA 基础培训教程-从实践中学习oracleDBA
- 精彩对白(笑话)
- UML学习手记(二):用例分析之使用叙述
- Dos script(1)
- asp.net验证码
- Jsp/Java代码分离.实现页面真正的代码分离 设计原理