如果有<%@ page contentType="text/html;charset=gb2312"%>,插入中文数据时一般不用再进行换码
UnicodeToChinese:
str = new String(s.getBytes("ISO8859_1"), "gb2312");
ChineseToUnicode:
str = new String(s.getBytes("gb2312"),"ISO8859_1");
UnicodeToChinese:
str = new String(s.getBytes("ISO8859_1"), "gb2312");
ChineseToUnicode:
str = new String(s.getBytes("gb2312"),"ISO8859_1");
<%@ page contentType="text/html;charset=gb2312"%>然后对你传过来的字符串进行如下处理
str = new String(s.getBytes("ISO8859_1"), "gb2312");
<%@ page contentType="text/html; charset=gb2312" %>
或者在servlet里面
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html; charset=gb2312");
上面的如果在不行就用如下的方法在数据入库前进行调用:
public static String UnicodeToChinese(String s){
try{
if(s==null||s.equals("")) return "";
String newstring=null;
newstring=new String(s.getBytes("ISO8859_1"),"gb2312");
return newstring;
}
catch(UnsupportedEncodingException e)
{
return s;
}
}public static String ChineseToUnicode(String s){
try{
if(s==null||s.equals("")) return "";
String newstring=null;
newstring=new String(s.getBytes("gb2312"),"ISO8859_1");
return newstring;
}
catch(UnsupportedEncodingException e)
{
return s;
}
}
public String getStr(String str)//中文处理
{
try
{
String temp_p=str;
byte[] temp_t=temp_p.getBytes("ISO8859-1");
String temp=new String(temp_t);
return temp;
}
catch(Exception e)
{
e.printStackTrace() ;
}
return "null";
}