中文乱码的解决方案:
来源:互联网 发布:开源博客系统 php 编辑:程序博客网 时间:2024/05/16 09:54
1.以POST请求方式:
在获取请求参数之前设置: request.setCharacterEncoding(“utf-8”);
设置输出编码:
或者<%@ page> contentType=”text/html;charset=utf-8” %>
2.以GET方式请求的
以GET方式请求的设置setCharacterEncoding是无效的。
我们需要在获取参数后,对每一个参数进行单独转码。
String name =request.getParameter(“name”);
name=new String(name.getBytes(“ISO-8859-1”),”utf-8”);
3.在数据库中的乱码:
设置数据库的编码为utf-8
4.使用过滤器解决中文乱码问题:
a.使用动态代理可以解决。本人亲测,直接上代码;
package cn.itcast.filter;import java.io.IOException;import java.lang.reflect.InvocationHandler;import java.lang.reflect.Method;import java.lang.reflect.Proxy;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.HttpServletResponse;public class EncodeFilter implements Filter { public void destroy() { } public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException { System.out.println("-----------进入了过滤器------------"); // 转换 final HttpServletRequest request = (HttpServletRequest) req; HttpServletResponse response = (HttpServletResponse) resp; // 设置post编码解决 request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); final String method1 = request.getMethod(); System.err.println("访问方式:"+method1); // 动态代理 HttpServletRequest proxy = (HttpServletRequest) Proxy.newProxyInstance( request.getClass().getClassLoader(), new Class[] { HttpServletRequest.class }, new InvocationHandler() { public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { Object invoke =null; if(method1.equals("GET") && "getParameter".equals(method.getName())){ //如果是get方式然后就 invoke = (String)method.invoke(request, args); invoke = new String(((String) invoke).getBytes("ISO-8859-1"),"utf-8"); System.out.println("转换后的"+invoke); }else{ invoke = method.invoke(request, args); } return invoke; } }); // 放行 chain.doFilter(proxy, response); } public void init(FilterConfig arg0) throws ServletException { }}
b.还有一种就是网上很多的重写getParameter方法,有空过来补上;
5.上传与下载的乱码或中文不显示问题解决:
JSP或Servlet上传文件中文名乱码解决:
String fileN=item.getName();//String fileName=new String(fileN.getBytes("ISO-8859-1"),"utf-8");String fileName=new String(fileN.getBytes(),"utf-8");//对提取出来的文件名进行UTF-8编码,
以上可以解决上传文件名乱码和浏览器显示以及存入数据库都显示正常。
解决下载文件时中文不显示的问题:
response.setContentType("application/x-msdownload");response.setHeader("Content-Disposition", "attachment;filename="+new String(fileName.getBytes("utf-8"),"ISO-8859-1"));
原因分析,由于HTTP头部的默认编码为ISO-8859-1而我们上传文件于下载文件过程中,提取到的文件名都要通过HTTP头部。
所以我们需要在上传的时候对提取到的文件名进行转码为UTF-8,然后在下载时我们也要进行反向转码为ISO-8859-1.
0 0
- 中文乱码的解决方案:
- JSP中文乱码的解决方案
- J2EE中文乱码的解决方案
- Wine 中文乱码的解决方案
- ubuntu中文乱码的解决方案
- OFBiz 的中文乱码解决方案
- JFreeChart中文乱码的解决方案
- OFBiz 的中文乱码解决方案
- OFBiz 的中文乱码解决方案
- HttpClient中文乱码的解决方案
- JSP的中文乱码解决方案
- 中文乱码问题的解决方案
- 中文乱码问题的解决方案
- OFBiz 的中文乱码解决方案
- 完美的中文乱码解决方案
- JFreeChat 中文乱码、汉字乱码的解决方案
- JFreeChat 中文乱码、汉字乱码的解决方案
- Tomcat的中文乱码的经典解决方案
- POJ2386 Lake Counting(dfs)
- LINQ to SQL活学活用(4):监视你的一举一动
- imageview的scaleType详解(附图)
- OpenGL ES 2.0 - 几种剪裁与测试
- 统一编址&独立编址&哈佛结构&冯诺依曼结构
- 中文乱码的解决方案:
- 第五章
- JQuery遍历获取table中的第一个和第四个孙子,并把第四个孙子赋值给另一个table
- cannot resolve symbol controller
- 【认证】【UK】UK FreeviewHD/Freeview+HD/FreesatHD
- 解读zookeeper的配置项
- html5移动开发-在IOS在keyup和change无法获取中文
- bios升级后读取不到u盘怎么办
- 设置生成.exe的图标