java数据库等——乱码问题解决办法
中文乱码解决方案:
数据库乱码:
UTF-8对中文用3个字节来表示
String s =”飞翔”;
byte[] utf8 = s.getBytes(“utf-8”);//获得utf-8编码的字节值
System….(new String(utf8,”gbk”);//用GBK编码格式对UTF-8编码格式的字节数组进行解码,将产生乱码。
查看数据库的字符集
show variables like 'character\_set\_%';
show variables like 'collation_%';
--设置数据库字符编码
set names 'GBK'
alter database payment character set GBK;
create database mydb character set GBK;
set character_set_client=gbk;
set character_set_connection=gbk;
set character_set_database=gbk;
set character_set_results=gbk;
set character_set_server=gbk;
set character_set_system=gbk;
set collation_connection=gbk;
set collation_database=gbk;
set collation_server=gbk;
Servlet解决服务器端传给客户端产生的乱码问题
添加GB2312编码
向服务器输出中文时出现乱码或“?”最常用的解决方案:
service(HttpServletRequest request,HttpServletResponse response){
String s =” 中文”;
response.setContentType(“text/html;charset=utf-8”);
PrintWriter out = response.getWriter();
out.println(s);
}
以上方法有时候会失灵,所以使用下面试试:
service(){
String s = “中文”;
PrintWriter out = response.getWriter();
response.setHeader(“Content-Type”,”text/html;charset=utf-8”);//设置Conten-Type响应头
out.println(new String(s.getByte(“utf-8”),”iso-8859-1”));
}//获得utf-8编码的字节数组后,将其按原样保存在String对象中
}
1struts:
String name =request.getParameter("username");
String username;
try {
username=new String(request.getParameter("username").getBytes("ISO-8859-1"), "gb2312" );
username = new String(username.getBytes(“ISO8859-1”),”GBK”);
2 struts2
HttpServletRequest request=ServletActionContext.getRequest();String
str=new Strin
相关文档:
Java NIO API详解
在JDK
1.4以前,Java的IO操作集中在java.io这个包中,是基于流的阻塞(blocking)API。对于大多数应用来说,这样的API使用很方
便,然而,一些对性能要求较高的应用,尤其是服务端应用,往往需要一个更为有效的方式来处理IO。从JDK 1.4起,NIO
API作为一个基于缓冲区,并能提供非阻塞(non-blo ......
1.用new String(request.getParameter("name").getBytes("ISO-8859-1"),"GBK") 方式进行转码
2.设置tomcat:在tomcat的conf目录下找到server.xml文件,在Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
......
/*
* EncryptUtils.java
* Copyright (C) 2007-3-19 <JustinLei@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* ......
package org.lambdasoft.web.support;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import org.lambdasoft.web.Enviroment;
public class SessionSupport {
private SessionS ......
/*
* CookieSupport.java
* Copyright (C) 2007-3-19 <JustinLei@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version ......