Java 后台向前台传递中文乱码

来源:互联网 发布:服装商品数据分析公式 编辑:程序博客网 时间:2024/05/22 02:20

在这里提供一个函数,通过这个函数来发送信息,就不会出现乱码,核心思想也是设置response流的字符集。函数代码如下:

Java代码  收藏代码
  1. /** 
  2.  * @Function:writeResponse 
  3.  * @Description:ajax方式返回字符串 
  4.  * @param str:json 
  5.  * @return:true:输出成功,false:输出失败 
  6.  */  
  7. public boolean writeResponse(String str){  
  8.     boolean ret = true;  
  9.     try{  
  10.         HttpServletResponse response = ServletActionContext.getResponse();  
  11.         response.setContentType("text/html;charset=utf-8");  
  12.         PrintWriter pw = response.getWriter();  
  13.         pw.print(str);  
  14.         pw.close();  
  15.     }catch (Exception e) {  
  16.         ret = false;  
  17.         e.printStackTrace();  
  18.     }  
  19.     return ret;  
  20. }     
原创粉丝点击