1如果使用的tomcat服务器,在server.xml中Connector 标签后加 URIEncoding="UTF-8";

2使用web过滤器:

(1)、新建一个SetCharacterEncodingFilter.java的类:

package com.util;
import java.io.IOException;
import javax.servlet.*;

public class SetCharacterEncodingFilter 
implements Filter{
 protected String encoding = null;

protected FilterConfig filterConfig =
null;

protected boolean ignore = true;

public void destroy()
 {
   
this.encoding = null;
   
this.filterConfig = null;

}

public void doFilter(ServletRequest request,
ServletResponse response,
    
FilterChain chain) throws IOException, ServletException
 {
    if (ignore
|| (request.getCharacterEncoding() == null))
    {
    
String encoding = selectEncoding(request);
    
if (encoding != null)
     
request.setCharacterEncoding(encoding);
    }
   
chain.doFilter(request, response);
 }

public void init(FilterConfig filterConfig)
throws ServletException
 {
   
this.filterConfig = filterConfig;
    //
获取初始化参数
   
this.encoding = filterConfig.getInitParameter("encoding");
    String value
= filterConfig.getInitParameter("ignore");
    if (value ==
null)
    {
    
this.ignore = true;
    } else if
(value.equalsIgnoreCase("true"))
    {
    
this.ignore = true;
    } else if
(value.equalsIgnoreCase("yes"))
    {
    
this.ignore = true;
    } else
    
this.ignore = false;
 }

protected String selectEncoding(ServletRequest
request)
 {
    return
(this.encoding);
 }

}

(2)web.xml中在web-app里加入:

<!--定义一个过滤器, 并设定其初始化参数--><filter>

<filter-name>Set Character
Encoding</filter-name>
  
<filter-class>com.util.SetCharacterEncodingFilter</filter-class>

<init-param>
   
<param-name>encoding</param-name>

<param-value>GB2312</param-value>

</init-param>
</filter>
<!--制定过滤器映射-->
<filter-mapping>
  
<filter-name>Set Character
Encoding</filter-name>
  
<url-pattern>/*</url-pattern>

</filter-mapping>

3.<%@ page language="java" import="java.util.*,com.scce.entity.*" pageEncoding="utf-8"%>

<head>

  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

</head>

4.String name=new String(request.getParameter("name").getBytes("ISO8859-1"),"GBK");

new String(fileName.getBytes("UTF-8"), "GBK")

5.request.setCharacterEncoding("gbk");

response.setContentType("text/html;charset=gbk");

java中的乱码问题的更多相关文章

  1. java中避免乱码

    response.setContentType("text/html;charset=UTF-8"); 这个是在action中的 这个是在json中设置乱码的 contentTyp ...

  2. java中byte, iso-8859-1, UTF-8,乱码的根源

    Post@https://ryan-miao.github.io 背景 还是多语言, 在项目中遇到本地环境和服务端环境不一致乱码的情形.因此需要搞清楚乱码产生的过程,来分析原因. 获取多语言代码如下: ...

  3. java中几种常见字符集与乱码介绍

    1.  ASCII和Ansi编码 字符内码(charcter code)指的是用来代表字符的内码 .读者在输入和存储文档时都要使用内码,内码分为  单字节内码 -- Single-Byte chara ...

  4. Java中url传递中文参数取值乱码的解决方法

    java中URL参数中有中文值,传到服务端,在用request.getParameter()方法,得到的常常会是乱码,这将涉及到字符解码操作. 方法一: http://xxx.do?ptname=’我 ...

  5. java中可以出现的中文乱码的集中解决

    从学习javaweb开始就会经常遇到中文乱码,今天就做以下记录: 1. 要避免项目中遇到乱码,首先就是在搭建项目的设置工作空间的字符编码,若是多人开发,就更应该做到统一,在eclipse中选择widn ...

  6. JAVA中传递参数乱码问题

    url传递中文如果jsp页面,myeclipse.web.xml中org.springframework.web.filter.CharacterEncodingFilter,都是UTF-8编码,直接 ...

  7. java中的中文参数存到数据库乱码问题

    关于java中的中文参数乱码问题,遇见过很多,若开发工具的字符集环境和数据库的字符集环境都一样,存到数据库中还是乱码的话,可以通过以下方法解决: 用数据库客户端检查每个字段的字符集和字符集校对和这个表 ...

  8. java乱码详解(java中byte与char的转换)

    转自:http://hi.baidu.com/%C6%F3%D2%B5%BC%D2%D4%B0/blog/item/825a4858d6248e8b810a181a.html   java byte与 ...

  9. Java中FTPClient上传中文目录、中文文件名乱码问题解决方法【好用】

    转: Java中FTPClient上传中文目录.中文文件名乱码问题解决方法 问题描述: 使用org.apache.commons.net.ftp.FTPClient创建中文目录.上传中文文件名时,目录 ...

随机推荐

  1. 四种读写方案IO流 (JAVA)

    File类用于访问文件或目录的属性 流:指一连串流动的字符,是以先进先出的方式发送信息的通道.程序和数据源之间是通过流联系起来的. 第一套:字节流读取写入方案 FileInputStream :字节流 ...

  2. LaTex Remove Left Margin 去除左边空间

    LaTex中使用itemize的时候,默认的每一项左边都有一小段距离,并不是紧贴着边缘的,那么如果我们想去掉这段距离,我们可以使用下面的命令: \usepackage{enumitem} \setli ...

  3. 关于http协议的理解

    一.状态码 1.200:请求成功. 2.302:浏览器进行重定向. 3.304:资源已使用,即有缓存. 4.404:请求失败,请求的资源未在服务器上发现. 5.500:服务器端发生错误. 二.php获 ...

  4. 前端性能优化----yahoo前端性能团队总结的35条黄金定律

    转自 http://www.cnblogs.com/lei2007/archive/2013/08/16/3262897.html

  5. HDU1058 DP

    Humble Numbers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  6. 2016huasacm暑假集训训练三 B-Frogger

    题目链接:http://acm.hust.edu.cn/vjudge/contest/123674#problem/B 题意:一只青蛙在湖中一颗石头上, 它想去有另一只青蛙的石头上,但是 湖里的水很脏 ...

  7. A+B Problem III-(涉及误差)NYOJ-477

    描述求A+B是否与C相等.   输入 T组测试数据. 每组数据中有三个实数A,B,C(-10000.0<=A,B<=10000.0,-20000.0<=C<=20000.0) ...

  8. Web前端开发基础 第四课(CSS元素模型)

    css布局模型 清楚了CSS 盒模型的基本概念. 盒模型类型, 我们就可以深入探讨网页布局的基本模型了.布局模型与盒模型一样都是 CSS 最基本. 最核心的概念. 但布局模型是建立在盒模型基础之上,又 ...

  9. 【转】C#(ASP.Net)获取当前路径的方法集合

    转自:http://www.gaobo.info/read.php/660.htm //获取当前进程的完整路径,包含文件名(进程名). string str = this.GetType().Asse ...

  10. shader三种变量类型(uniform,attribute和varying)

    uniform变量在vertex和fragment两者之间声明方式完全一样,则它可以在vertex和fragment共享使用.(相当于一个被vertex和fragment shader共享的全局变量) ...