文件下载的字节显示为纯文本而不是实际附件

时间:2014-08-12 19:33:29

标签: jsf download seam2

我想我已把所有部分放在一起提供下载文件。相反,下面的代码是在浏览器本身上写字节数组。我确信byte []没有任何问题,因为我之前使用FileOutputStream来生成excel文件。

    FacesContext fc = FacesContext.getCurrentInstance();
    HttpServletResponse response = (HttpServletResponse)fc.getExternalContext().getResponse();

    InputStream templateInputStream=null;

    try {
        templateInputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(SOME_PATH);
        if(templateInputStream==null) {
            throw new WebException("Template not found at: "+SOME_PATH);
        }

        byte[] suppData= ExcelRenderer.render(templateInputStream, data);

        response.reset();
        response.setContentType("application/octet-stream");
        response.setHeader("Content-Disposition","attachment;filename=\""+REPORT_FILE_NAME+"\"");

        response.getOutputStream().write(suppData);

        response.flushBuffer();
        fc.responseComplete();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    finally{

        if (templateInputStream!=null){
            templateInputStream.close();
        }
    }

0 个答案:

没有答案