其實(shí)發(fā)現(xiàn)也沒(méi)什么好說(shuō)的。

只是注意如果這句話(huà)彈不下載的提示,除了要設(shè)置response之外
response.setCharacterEncoding("utf-8");
response.setContentType("multipart/form-data");
response.addHeader("Content-Disposition",
"attachment;filename="+strName);
還需要注意請(qǐng)求的方式,如果用jQuery的ajax請(qǐng)求是不行的,網(wǎng)上解釋的大概意思應(yīng)該是ajax不能正確傳入response對(duì)象這樣的,所以發(fā)起請(qǐng)求應(yīng)該用<a>標(biāo)簽,或者<form>的action屬性吧。
StringBuffer fileName = new StringBuffer(year+"年"+month+"月歸檔情況統(tǒng)計(jì).xls");
String strName = null;
//處理瀏覽器不同下載的文件名亂碼問(wèn)題
try {
String agent = request.getHeader("User-Agent");
boolean isMSIE = ((agent != null && agent.indexOf("MSIE") != -1 ) || ( null != agent && -1 != agent.indexOf("like Gecko")));
if (isMSIE) {
strName = URLEncoder.encode(fileName.toString(), "UTF-8");
}
else {
strName = new String(fileName.toString().getBytes("UTF-8"),"ISO8859-1");
}
} catch (Exception e) {
e.printStackTrace();
}
這段代碼也超級(jí)有用,下載你會(huì)發(fā)現(xiàn)文件名不能用中文,這時(shí)候就需要上面這段代碼了。