今天项目中用到了写入流下载。代码如下。
HttpServletResponse reponse = super.getResponse();
reponse.setHeader("Content-Disposition", "attachment;filename=" + fileName);
reponse.setContentType("application/octet-stream");
reponse.setBufferSize(2048);
reponse.setContentLength(byteArr.length);
ServletOutputStream out = null;
try {
out = reponse.getOutputStream();
out.write(byteArr, 0, byteArr.length);
} catch (IOException e1) {
// TODO Auto-generated catch block
log.error(e1);
}
这段代码在项目中一直运行良好。。近日,有位兄弟,在这里面加入了点点代码。。
导致在 chorme 浏览器中,抛出异常。
异常如下
收到了来自服务器的重复标头
来自服务器的响应包含重复标头。此问题通常是由于网站或代理配置不正确导致的。只有网站或代理管理员才能解决此问题。
错误 349 (net::ERR_RESPONSE_HEADERS_MULTIPLE_CONTENT_DISPOSITION):我们收到了多个“Content-Disposition”标头。我们不允许此行为,以防遭到 HTTP 响应拆分攻击。
但使用其他IE8,IE9,firefox 都运行良好。。
于上上网一搜,,还真搜出一堆。。解决方法如下
原因是因为下载文件名中包含有逗号分隔符。。在所有的浏览器中,下载文件名不支持双字节字符。
参考请见 http://greenbytes.de/tech/tc2231/#attmultinstances
Content-Disposition: attachment; filename=foo,bar.html
^ (PARSE ERROR)
Test Results | |
---|---|
FF11 | warn (accepts the unquoted value) |
FF14 | warn (accepts the unquoted value) |
MSIE8 | warn (accepts the unquoted value) |
MSIE9 | warn (accepts the unquoted value) |
Opera | warn (accepts the unquoted value) |
Safari | warn (treats the comma as delimiter and offers to download "foo.html") |
Konq | pass (ignores thes header field) |
Chr17 | pass (reports a network error ("Duplicate headers received from server")) |
Chr18 | pass (reports a network error ("Duplicate headers received from server")) |
'attachment', specifying a filename of foo,bar.html
using a comma despite using token syntax.