java中的乱码问题
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中的乱码问题的更多相关文章
- java中避免乱码
response.setContentType("text/html;charset=UTF-8"); 这个是在action中的 这个是在json中设置乱码的 contentTyp ...
- java中byte, iso-8859-1, UTF-8,乱码的根源
Post@https://ryan-miao.github.io 背景 还是多语言, 在项目中遇到本地环境和服务端环境不一致乱码的情形.因此需要搞清楚乱码产生的过程,来分析原因. 获取多语言代码如下: ...
- java中几种常见字符集与乱码介绍
1. ASCII和Ansi编码 字符内码(charcter code)指的是用来代表字符的内码 .读者在输入和存储文档时都要使用内码,内码分为 单字节内码 -- Single-Byte chara ...
- Java中url传递中文参数取值乱码的解决方法
java中URL参数中有中文值,传到服务端,在用request.getParameter()方法,得到的常常会是乱码,这将涉及到字符解码操作. 方法一: http://xxx.do?ptname=’我 ...
- java中可以出现的中文乱码的集中解决
从学习javaweb开始就会经常遇到中文乱码,今天就做以下记录: 1. 要避免项目中遇到乱码,首先就是在搭建项目的设置工作空间的字符编码,若是多人开发,就更应该做到统一,在eclipse中选择widn ...
- JAVA中传递参数乱码问题
url传递中文如果jsp页面,myeclipse.web.xml中org.springframework.web.filter.CharacterEncodingFilter,都是UTF-8编码,直接 ...
- java中的中文参数存到数据库乱码问题
关于java中的中文参数乱码问题,遇见过很多,若开发工具的字符集环境和数据库的字符集环境都一样,存到数据库中还是乱码的话,可以通过以下方法解决: 用数据库客户端检查每个字段的字符集和字符集校对和这个表 ...
- java乱码详解(java中byte与char的转换)
转自:http://hi.baidu.com/%C6%F3%D2%B5%BC%D2%D4%B0/blog/item/825a4858d6248e8b810a181a.html java byte与 ...
- Java中FTPClient上传中文目录、中文文件名乱码问题解决方法【好用】
转: Java中FTPClient上传中文目录.中文文件名乱码问题解决方法 问题描述: 使用org.apache.commons.net.ftp.FTPClient创建中文目录.上传中文文件名时,目录 ...
随机推荐
- Unique Binary Search Trees
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- ETL
1.ETL的定义 ETL分别是“Extract”.“ Transform” .“Load”三个单词的首字母缩写也就是“抽取”.“转换”.“装载”,但我们日常往往简称其为数据抽取. ETL是BI/DW ...
- TestStand与LabVIEW UI 交互
交互起因 客户觉得TestStand界面复杂,希望一个简单的界面即可进行序列执行,采用LabVIEW调用TestStand引擎可实现快速设计,将TestStand拆解到LabVIEW.然而,这样做需要 ...
- unity3d插件Daikon Forge GUI 中文教程4-高级控件Checkbox和dropdownlis的使用
第三节 高级的控件 Checkbox单选和多选.dropdownlis下拉列表.listbox列表框.progressbar进度条.richtextlabel富文本.slider滑块.textbox ...
- js中获取键盘事件【转】
<script type="text/javascript" language=JavaScript charset="UTF-8"> 2 docu ...
- Mysql的实时同步 - 双机互备
设置方法: 步一设 A 服务服 (192.168.1.43) 上用户为 backup, 123456 , 同步的数据库为test; B 服务服 (192.168.1.23) 上用户为 root, 12 ...
- 【HDU4612】 双连通分量求桥
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4612 题目大意:给你一个无向图,问你加一条边后最少还剩下多少多少割边. 解题思路:好水的一道模板题.先 ...
- Population Mean
Probability and Statistics > Moments > History and Terminology > Disciplinary Terminology & ...
- 【译】在ASP.Net和IIS中删除不必要的HTTP响应头
引入 每次当浏览器向Web服务器发起一个请求的时,都会伴随着一些HTTP头的发送.而这些HTTP头是用于给Web服务器提供一些额外信息以便于处理请求.比如说吧.如果浏览器支持压缩功能,则浏览器会发送A ...
- 《Linux内核设计与实现》CHAPTER1,2阅读梳理
<Linux内核设计与实现>CHAPTER1,2阅读梳理 [学习时间:2.5hours] [学习内容:Linux内核简介——历史与现今版本:Linux内核源代码以及编译] CHAPTER1 ...