给你一个转码类,我经常遇到转码,都是这个类处理的,嘿嘿,好用呢,楼主,一定要给分啊!
package db;import java.io.UnsupportedEncodingException;public class ChangeChar { public ChangeChar() {
} public static String Char(String str) {
String value = "";
String check = System.getProperty("os.name").toLowerCase();
if (str != null)
try {
if (check.indexOf("win") != -1) {
value = new String(str.getBytes("ISO-8859-1"));
} else {
value = new String(str.getBytes(), "GB2312");
}
} catch (UnsupportedEncodingException ex) {
ex.printStackTrace();
}
return value;
} public String gb2iso(String qs) {
try {
if (qs == null)
return "NULL";
else
return new String(qs.getBytes("gb2312"), "iso-8859-1");
} catch (Exception e) {
}
return "NULL";
}
public String Convert(String s){ String result = null; byte[] temp = null ; try {
temp = s.getBytes("iso-8859-1");
} catch (UnsupportedEncodingException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
} try {
result = new String(temp,"utf-8");
} catch (UnsupportedEncodingException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
return result; }
public String Convert1(String s){ String result = null; byte[] temp = null ; try {
temp = s.getBytes("utf-8");
} catch (UnsupportedEncodingException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
} try {
result = new String(temp,"iso-8859-1");
} catch (UnsupportedEncodingException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
return result; }}
package db;import java.io.UnsupportedEncodingException;public class ChangeChar { public ChangeChar() {
} public static String Char(String str) {
String value = "";
String check = System.getProperty("os.name").toLowerCase();
if (str != null)
try {
if (check.indexOf("win") != -1) {
value = new String(str.getBytes("ISO-8859-1"));
} else {
value = new String(str.getBytes(), "GB2312");
}
} catch (UnsupportedEncodingException ex) {
ex.printStackTrace();
}
return value;
} public String gb2iso(String qs) {
try {
if (qs == null)
return "NULL";
else
return new String(qs.getBytes("gb2312"), "iso-8859-1");
} catch (Exception e) {
}
return "NULL";
}
public String Convert(String s){ String result = null; byte[] temp = null ; try {
temp = s.getBytes("iso-8859-1");
} catch (UnsupportedEncodingException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
} try {
result = new String(temp,"utf-8");
} catch (UnsupportedEncodingException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
return result; }
public String Convert1(String s){ String result = null; byte[] temp = null ; try {
temp = s.getBytes("utf-8");
} catch (UnsupportedEncodingException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
} try {
result = new String(temp,"iso-8859-1");
} catch (UnsupportedEncodingException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
return result; }}
解决方案 »
- 十万火急,JBuilder下项目移植到Eclipse后jsp出错
- eclipse中第二次提交自动加入/servlet
- applet 如何加载第三方jar包
- 初学struts 的小小问题,望好心人在个忙. 空指针....
- 用户权限--急
- 介绍几本实战的书,兄弟们
- 什么叫标头
- mysql中如何查询中只显示某一字段的前10个字符,select 语句该如何写。
- 谁有介绍如何在JB7下调试(或断点调试)JSP文件的电子文档或资料,请给我发一份,不胜感激!
- 使用原生Mybatis如何取消事务回滚
- 导出excel报表的问题,大家来看一下~~
- struts spring整合问题 大哥帮帮忙啊,我贡献出全部分求解
<%request.setCharacterEncoding("utf-8");%>
2.直接转换
String strother = "可以把is08859-1转换成gb2312!";
String strchina = new String(strother.getBytes("iso8859-1"),"gb2312");
3.tomcat 中设置
<Connector port="8009"
enableLookups="false" redirectPort="8443" protocol="AJP/1.3" uRIEncoding="gb2312" />
所有编码都统一用UTF-8
1.页面用UTF-8
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<META http-equiv=Content-Type content="text/html; charset=UTF-8">
2.request过来的也要用UTF-8进行编码
String domain = BaseFunc.RequestForm(request,"domain","");
domain=new String(domain.getBytes("iso-8859-1"),"UTF-8")
3.数据库要用UTF-8编码
create TABLE `t_diary_domain_zhcn` (
`DATE` date NOT NULL,
`DOMAIN` text,
PRIMARY KEY (`DATE`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
4.数据库连接串要用UTF-8编码
url="jdbc:mysql://localhost/domain?useUnicode=true&characterEncoding=UTF-8"
5.从数据库取出的数据也要用UTF-8编码
domain=new String(domain.getBytes("iso-8859-1"),"UTF-8");