配置文件:
${downloadContentType}
downloadStream
user;filename="${downloadFileName}"
4096
后台代码:
downloadContentType = "application/vnd.ms-excel;charset=UTF-8";
downloadFileName = java.net.URLEncoder.encode(title, "UTF-8") +".xls";
ByteArrayOutputStream output = new ByteArrayOutputStream();
wb.write(output);
downloadStream = new ByteArrayInputStream(output.toByteArray());
return "download";
如图:

为了实现迅雷下载时显示真正的文件名,将后台代码改成:
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("application/octet-stream");
response.setHeader("content-disposition", "attachment;filename=" + java.net.URLEncoder.encode(title, "UTF-8") + ".xls");
String downloadPath = request.getSession().getServletContext().getRealPath("/WEB-INF/downloads");
Date now = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
FileOutputStream output = new FileOutputStream(downloadPath + "/" + sdf.format(now) + ".xls");
wb.write(output);
output.flush();
output.close();
//核心代码就下面这一行
request.getRequestDispatcher("/WEB-INF/downloads/" + sdf.format(now) + ".xls").forward(request, response);
return null;