Servlet写入文件,但不写入ServletOutputStream

时间:2018-08-01 16:40:51

标签: java servlets download

我正在从jsp到servlet进行ajax调用。

var xhr = new XMLHttpRequest();
function download(){
    xhr.open("GET", "/FileDownloader?<params>");
    xhr.send();
}

在servlet中,我正在创建一个Excel文件(POI HSSFWorkbook),可以成功将xls写入文件,但不能将其作为下载发送回浏览器:

private void ResultsXLS(HttpServletRequest req, HttpServletResponse resp) throws Exception {
    resp.setContentType("application/vnd.ms-excel");
    resp.setHeader("Expires", "0");
    resp.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
    resp.setHeader("Pragma", "public");
    resp.setHeader("Content-Disposition", "attachment; filename=export.xls");

    try{
        //Datbase query - populates workbook
        HSSFWorkbook wb = new HSSFWorkbook();

        //THIS WORKS - Excel file is flawless
            //try (FileOutputStream outputStream = new FileOutputStream("C:/TEMP/ic50s.xls")){
            //wb.write(outputStream)
            //outputStream.close();

        //THIS DOESN'T WORK
        ServletOutputStream out = resp.getOutputStream();   
        wb.write(out);
        out.flush();
        out.close();
    }
    catch (Exception e) {
        LOG.log(Level.SEVERE, e.toString(), e );
    } finally {
        //cleanup
    }
}

我在任何地方都看不到任何错误。我该如何进一步调试?

谢谢!

0 个答案:

没有答案