servlet返回值乱码
浏览器发送请求,获得一堆乱码。不过在控制台输出不是乱码,小程序获得也是正确的内容。
response的代码修改如下,
response.setContentType("text/plain;charset=utf-8");request.setCharacterEncoding("utf-8");
或者更全的写法
response.setCharacterEncoding("utf-8");response.setHeader("Content-Disposition", "attachment; filename="+ URLEncoder.encode(fileName, "UTF-8"));response.setContentType("text/plain;charset=utf-8");PrintWriter out =response.getWriter();
这样写完,浏览器就获得正确的文字了。
注意 PrintWriter out =response.getWriter()一定要写在最后,不然response的配置可能不会生效
其他内容补充
response.setContentType(MIME)的作用是使客户端浏览器,区分不同种类的数据,并根据不同的MIME调用浏览器内不同的程序嵌入模块来处理相应的数据。
例如web浏览器就是通过MIME类型来判断文件是GIF图片。通过MIME类型来处理json字符串。
Tomcat的安装目录\conf\web.xml 中就定义了大量MIME类型 ,可以参考。
response.setContentType("text/html; charset=utf-8"); html
.setContentType("text/plain; charset=utf-8"); 文本
text/javascript json数据
application/xml xml数据
https://blog.csdn.net/ikigai_6/article/details/132529003
https://blog.csdn.net/luman1991/article/details/53423305
https://www.juhe.cn/news/index/id/9158
https://blog.csdn.net/ZHANGYANG_1109/article/details/121224271
https://blog.csdn.net/Nothing_Wzy/article/details/115730194

浙公网安备 33010602011771号