java servlet 乱码

来源:互联网 发布:科比王力宏小视频软件 编辑:程序博客网 时间:2024/06/14 19:15

java写文件乱码

StringBuffer sb = new StringBuffer();
  sb.append("<table  border=/"0/" width=/"100%/" cellpadding=/"0/" cellspacing=/"1/" bgcolor=/"b5d6e6/"");
  sb.append("<tr>");
  sb.append("<td bgcolor=/"#FFFFFE/" height=/"42/" colspan=/""+ (jsd.size()+1) +"/" align=/"center/"><b>" +    dataSet.getTitle() + "</b></td>");
  sb.append("</tr>");

  sb.append("</tr>");
  sb.append("</table>");
  return sb.toString();

 

解决方法

使用

public static String ISOToInnerCode(String str) {
        if (str == null)
            return null;

        try {
            str = (new String(str.getBytes("ISO-8859-1"), "UTF-8")).trim();
        } catch (Exception ex) {
        }

        return str;
    }

 

若是在servlet中则使用

request.setCharacterEncoding("UTF-8");
   response.setContentType("text/html;charset=utf-8"); 
    PrintWrite out=response.getWrite();

如果是

OutputStream out = response.getOutputStream();

输出时用out.write(Content.getBytes("utf-8"));即增加一种编码 utf-8也可改成其他编码gbk,gb2312等 依自己情况而定

 

如果接受传参数时乱码 只需把 response.setContentType("text/html;charset=utf-8"); 
改成response.setCharacterEncoding("UTF-8");

 

 

 

原创粉丝点击