1

我有一个 jsf 应用程序,当用户单击一个按钮时,有一个支持 bean 方法调用这个 servlet。

问题是在IE7和IE8中,文件无法下载。它给出了错误消息:

Internet Explorer cannot download ExcelGenerator from mydomain.com

我需要做什么才能让它在 IE7/IE8 中工作?我需要设置一些额外的标题吗?

调用 servlet 的支持 bean 方法:

public void generateExcel()
{
    FacesContext context = FacesContext.getCurrentInstance();
    String url = context.getExternalContext().getRequestContextPath() + "/ExcelGenerator";
    context.getExternalContext().redirect(url);
}

生成 excel 文件的 Servlet:

 public class ExcelGenerator extends HttpServlet {

        private static final long serialVersionUID = 1L;
        private static final Logger LOG = LoggerFactory.getLogger(ExcelGenerator.class);

        @Override
        public void init(ServletConfig config) throws ServletException {
            super.init(config);
        }

        @SuppressWarnings("unchecked")
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

            SXSSFWorkbook wb = new SXSSFWorkbook();
            Sheet sheet = wb.createSheet("Results");
            List<List<TableData>> results = (List<List<TableData>>) request.getSession().getAttribute("results");
            String tableName = (String) request.getSession().getAttribute("tableName");
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            Date date = new Date();
            String filename = tableName+"-"+df.format(date)+".xlsx";
            OutputStream out = null;

            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setHeader("Expires:", "0"); // eliminates browser caching
            response.setHeader("Content-Disposition", "attachment; filename="+filename);

            int rowCounter = 0;

            // Create the rows
            for(List<TableData> l : results) {

                int counter = 0;
                Row row = sheet.createRow(rowCounter);

                for(TableData td : l) {

                    Cell cell = row.createCell(counter);

                    // if we're on the first row, get the column description, else get the data
                    if(rowCounter == 0) {
                        cell.setCellValue(td.getColumnDescription());
                    } else {
                        cell.setCellValue(td.getData());
                    }
                    counter++;
                }
                rowCounter++;
            }

            try {
                out = response.getOutputStream();
                wb.write(out);
                out.flush();
            } catch (Exception e) {
                LOG.debug(e.getStackTrace().toString());
            } finally {
                out.close();
                wb.dispose();
            }
        }
    }
4

3 回答 3

3

Internet Explorer 无法从 mydomain 下载 ExcelGenerator。

当域使用 HTTPS 并且响应不允许缓存时,此错误很典型。另见MS 知识库 316431

修复相当简单:

if (request.isSecure()) {
    response.setHeader("Cache-Control", "public");
    response.setHeader("Pragma", "public");
}

也可以看看:

于 2013-02-05T16:05:35.603 回答
0

看看这个页面,它列出了最近浏览器版本与Content-Disposition标题的兼容性。某些值的组合,尤其是围绕文件名部分的编码方式,可能会导致 IE 显示错误。

于 2013-02-05T16:02:37.703 回答
0

我遇到了这个问题,下面的条目解决了这个问题

response.setContentType("application/vnd.ms-xls; charset=utf-8");

response.setHeader("内容配置","附件;文件名="+文件名);

response.setHeader("Pragma", "public");

response.setHeader("Cache-Control", "public, max-age=3800");

于 2015-05-01T14:39:51.530 回答