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. [LintCode] Maximal Rectangle 最大矩形

    Given a 2D boolean matrix filled with False and True, find the largest rectangle containing all True ...

  2. json 输出中文乱码解决办法

    echo json_decode(json_encode("修改成功")); 这样就行了

  3. oauth2.0了解

    http://www.ruanyifeng.com/blog/2014/05/oauth_2_0.html

  4. Oracle常用命令(持续更新)

    --1.解锁用户 alter user 用户名 account unlock; --2.开启最小补充日志记录(执行的DML操作会被记录下来)  alter database add supplemen ...

  5. iOS 如何通过CocoaPods添加第三方框架

    一  先安装Ruby环境: http://ruby-china.org/wiki/install_ruby_guide 在安装的时候,若是出现: 1.You don't have write perm ...

  6. mysql批量替换指定字符串

    将字段title中的abc替换为123 UPDATE `table1` SET `title` = replace(title, 'abc', '123') WHERE `typeid` =18;

  7. 实验一Java开发环境的熟悉

    实验一Java开发环境的熟悉 实验内容 •命令行下Java程序开发 •IDEA下Java程序开发.调试 •练习(通过命令行和Eclipse两种方式实现,在Eclipse下练习调试程序) •实现凯撒密码 ...

  8. php课程---Windows.open()方法参数详解

    Window.open()方法参数详解 1, 最基本的弹出窗口代码   window.open('page.html'); 2, 经过设置后的弹出窗口   window.open('page.html ...

  9. EL表达式与JSTL

    内容包括 EL表达式 EL函数库 JSTL 核心标签库 格式化标签库 SQL标签库 XML标签库 自定义标签库 EL表达式 EL是Expression Language的是缩写,是JSP页面编写的一种 ...

  10. 【SIGGRAPH】用【有说服力的照片真实】技术实现最终幻想15的视觉特效

    原文:西川善司 http://www.4gamer.net/games/075/G007535/20160726064/   最终幻想15的演讲会场.相当大,听众非常多.      在本次计算机图形和 ...