关于http响应附加额外字符ie

时间:2014-02-10 11:53:38

标签: java http

我在Chrome浏览器的Chrome浏览器中遇到http响应的问题,它运行正常。 当我将HttpResponse设置为:

response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
response.getWrite().write("xyz");
response.getWrite().flush();
response.getWrite().close();

显示的浏览器响应如下: 3 xyz0 数字字符附加在字符串的begening和end。

如何删除多余的字符3和0,问题仅存在于IE

1 个答案:

答案 0 :(得分:3)

尝试设置响应的Content-length标头。好像您的servlet容器正在将响应数据发送为chunked,字符“3”(以十六进制值表示的块中数据的八位数)和“0”(最后一个块)是如果响应中没有content-length标题,则会收到数据,请尝试此操作

String content = "xyz";
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
response.setContentLength(content.getBytes().length);
response.getWriter().write(content);
response.getWriter().flush();
response.getWriter().close();