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. android-数据存储之外部file存储(sdcard)

    一.基础概要 1.说明: 1>应用程序运行用到的数据文件可以保存到sd卡中 2>文件类型:任意 3>数据保存路径: 路径1:/storage/sdcard/Android/data/ ...

  2. border-radius

  3. BootStrap 模态框基本用法

    <!DOCTYPE html> <html> <head> <title>Bootstrap 实例 - 模态框(Modal)插件方法</title ...

  4. sign in和sign up区别

    如果是网站的话sign up是注册,sign in是登录的意思,另外,sign out退出

  5. SpringMVC如何接收application/json内容编码类型的参数?

    在上代码之前,有必要先说说@ResquestBody注解的含义: 1.官方解释如下: Annotation indicating a method parameter should be bound ...

  6. XML真正强大的功能是来自其元素与封装的内容

    创建文档类型声明 一般而言,XML声明放在文档顶部.在PHP中声明十分简单:只需实例化一个DOM文档类的对象并赋予它一个版本号.查看程序清单A: 程序清单 A <?php// create do ...

  7. isset 和empty 两个函数的用法

    关于用php 获取当前脚本的url很多朋友会说很简单,但是要获取很详细的就要经过多次判断哦. $PHP_TIME = time();$PHP_SELF = isset($_SERVER['PHP_SE ...

  8. love your life

    However mean your life is, meet it and live it ;do not shun it and call it hard names. It is not so ...

  9. 一个应用层的Makefile

    CC = gcc #gcc编译器LIB= -lpthread #需要链接的库文件CFLAGS=-std=gnu99 #C编译器的选项,C99标准OBJ=test.o gpio.o #生成的汇编文件PR ...

  10. MBR(Master Boot Record)主引导记录分析

    root@ubuntu1404:/home/chen# fdisk -l /dev/sda1 Disk /dev/sda1: MB, bytes heads, sectors/track, cylin ...