当前位置: 首页 > 知识库问答 >
问题:

如何修复“已为此响应调用了GetOutputStream()”

丁德义
2023-03-14

当我想将数据从数据库导出到excel文件时,我得到以下异常“java.lang.IllegalStateException:getOutputStream()已经被调用用于此响应”。

/****服务****/

@Component("articleExporter")
public class ArticleExporter implements FileExporter{

    @Autowired
    private IArticleService articleService;

    private static final String FILE_NAME="Liste des articles";

    @Override
    public boolean exportDataToExcel(HttpServletResponse response, String filename, String encodage) {
        if(org.apache.commons.lang3.StringUtils.isEmpty(filename)) {
            filename=FILE_NAME;

        }
        if(StringUtils.isEmpty(encodage)) {
            encodage=ApplicationConstants.DEFAULT_ENCODAGE;
        }
        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-Disposition", "attachment; filename="+ filename +".xls");
        WorkbookSettings workbookSettings = new WorkbookSettings();
        workbookSettings.setEncoding("ISO-8859-1");
        try {
            WritableWorkbook workbook = Workbook.createWorkbook(response.getOutputStream(), workbookSettings);
            WritableSheet sheet = workbook.createSheet(filename, 0);
            /**
             * Header of the table
             * */

            Label labelRubrique = new Label(0, 0, ApplicationConstants.RUBRIQUE);
            labelRubrique.setCellFeatures(new WritableCellFeatures());
            labelRubrique.getCellFeatures().setComment("");
            sheet.addCell(labelRubrique);

            Label labelFournisseur = new Label(1, 0, ApplicationConstants.FOURNISSEUR);
            labelFournisseur.setCellFeatures(new WritableCellFeatures());
            labelFournisseur.getCellFeatures().setComment("");
            sheet.addCell(labelFournisseur);

            Label labelcode = new Label(2, 0, ApplicationConstants.CODE);
            labelcode.setCellFeatures(new WritableCellFeatures());
            labelcode.getCellFeatures().setComment("");
            sheet.addCell(labelcode);

            Label labelReference = new Label(3, 0, ApplicationConstants.REFRENCE);
            labelReference.setCellFeatures(new WritableCellFeatures());
            labelReference.getCellFeatures().setComment("");
            sheet.addCell(labelReference);

            Label labelReferenceProtronic = new Label(4, 0, ApplicationConstants.REFERENCE_PROTRONIC);
            labelReferenceProtronic.setCellFeatures(new WritableCellFeatures());
            labelReferenceProtronic.getCellFeatures().setComment("");
            sheet.addCell(labelReferenceProtronic);

            Label labelDesignationL = new Label(5, 0, ApplicationConstants.DESIGNATION_L);
            labelDesignationL.setCellFeatures(new WritableCellFeatures());
            labelDesignationL.getCellFeatures().setComment("");
            sheet.addCell(labelDesignationL);

            Label labelDesignationC = new Label(6, 0, ApplicationConstants.DESIGNATION_C);
            labelDesignationC.setCellFeatures(new WritableCellFeatures());
            labelDesignationC.getCellFeatures().setComment("");
            sheet.addCell(labelDesignationC);

            Label labelPrixUnitaireHT = new Label(7, 0, ApplicationConstants.PRIX_UNIT_HT);
            labelPrixUnitaireHT.setCellFeatures(new WritableCellFeatures());
            labelPrixUnitaireHT.getCellFeatures().setComment("");
            sheet.addCell(labelPrixUnitaireHT);

            Label labelQuantite = new Label(8, 0, ApplicationConstants.QUANTITE);
            labelQuantite.setCellFeatures(new WritableCellFeatures());
            labelQuantite.getCellFeatures().setComment("");
            sheet.addCell(labelQuantite);

            Label labelPrixUnitaireTTC = new Label(9, 0, ApplicationConstants.PRIX_UNIT_TTC);
            labelPrixUnitaireTTC.setCellFeatures(new WritableCellFeatures());
            labelPrixUnitaireTTC.getCellFeatures().setComment("");
            sheet.addCell(labelPrixUnitaireTTC);

            Label labelTauxTVA = new Label(10, 0, ApplicationConstants.TAUX_TVA);
            labelTauxTVA.setCellFeatures(new WritableCellFeatures());
            labelTauxTVA.getCellFeatures().setComment("");
            sheet.addCell(labelTauxTVA);

            int currentRow =1;
            List<Article>  listArticles = articleService.selectAll();
            if(/*listArticles !=null &*/ !listArticles.isEmpty()) {
                /**
                 * Writing in the sheet
                 */
                for (Article article : listArticles) {

                    sheet.addCell(new Label(0,currentRow,article.getRubrique().getDesignation()));
                    sheet.addCell(new Label(1,currentRow,article.getFournisseur().getNomFournisseur()));
                    sheet.addCell(new Label(2,currentRow,article.getCodeArt()));
                    sheet.addCell(new Label(3,currentRow,article.getRefArt()));
                    sheet.addCell(new Label(4,currentRow,article.getRefArtPro()));
                    sheet.addCell(new Label(5,currentRow,article.getDesignationL()));
                    sheet.addCell(new Label(6,currentRow,article.getDesignationC()));
                    sheet.addCell(new Label(7,currentRow,article.getPrixUnitaireHT().toString()));
                    sheet.addCell(new Label(8,currentRow,article.getQuantite().toString()));
                    sheet.addCell(new Label(9,currentRow,article.getPrixUnitaireTTC().toString()));
                    sheet.addCell(new Label(10,currentRow,article.getTauxTva().toString()));
                    currentRow++;
                    }
                CellView cellView= new CellView();
                cellView.setAutosize(true);
                //cellView.setSize(600);
                sheet.setColumnView(0, cellView);
                sheet.setColumnView(1, cellView);
                sheet.setColumnView(2, cellView);
                sheet.setColumnView(3, cellView);
                sheet.setColumnView(4, cellView);
                sheet.setColumnView(5, cellView);
                sheet.setColumnView(6, cellView);
                sheet.setColumnView(7, cellView);
                sheet.setColumnView(8, cellView);
                sheet.setColumnView(9, cellView);
                sheet.setColumnView(10, cellView);

                /**
                 * Write to excel sheet
                 */

                workbook.write();

                /**
                 * Closing the workbook
                 */

                workbook.close();

                response.getOutputStream().flush();
                response.getOutputStream().close();

            }
            return true;

        }catch(Exception e){
             e.printStackTrace();
             return false;

        }

    }

/* the controller */

@RequestMapping(value="/export/")
public String ExportArticles(HttpServletResponse response) {

        exporter.exportDataToExcel(response, null, null);
        return "article/article";
}

/*错误*/

共有1个答案

颛孙玉石
2023-03-14

调用workbook.write()时;Workbook.Close();已在执行

response.getOutputStream().flush();
response.getOutputStream().close();

所以抛出了异常,移除那些行就可以了。

 类似资料: