servlet输出生成乱码 解决

PrintWriter out = response.getWriter();

response.setCharacterEncoding("utf-8");

response.setContentType("application/vnd.ms-excel;charset=UTF-8");

 response.addHeader("Content-Disposition", "attachment;filename="+ym+"(" +uu.getUsername()+").xls");

出现乱码的根源在于 获得Writer是要在response设置后获得...即PrintWriter out = response.getWriter();不能在response.setCharacterEncoding("utf-8"); 之前...正确如下

 

response.setCharacterEncoding("utf-8");

response.setContentType("application/vnd.ms-excel;charset=UTF-8");

 response.addHeader("Content-Disposition", "attachment;filename="+ym+"(" +uu.getUsername()+").xls");

PrintWriter out = response.getWriter();

 

 

你可能感兴趣的:(servlet)