用于编写excel文件的Java servlet在IE7 / IE8中导致错误

时间:2013-02-05 15:55:11

标签: java excel internet-explorer servlets download

我有一个jsf应用程序,当用户单击一个按钮时,有一个调用此servlet的辅助bean方法。

问题是在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();
            }
        }
    }

3 个答案:

答案 0 :(得分:3)

  

Internet Explorer无法从mydomain下载ExcelGenerator。

当域在HTTPS上并且响应不允许缓存时,此错误是典型的。另请参阅MS KB 316431

修复很简单:

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

另见:

答案 1 :(得分:0)

查看this page,其中列出了最新浏览器版本与Content-Disposition标头的兼容性。有某些值的组合,特别是关于如何编码文件名部分,可能导致IE显示错误。

答案 2 :(得分:0)

我遇到过这个问题,并在进入后解决了这个问题

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

response.setHeader( “内容处置”, “附件;文件名=” +文件名);

response.setHeader(“Pragma”,“public”);

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