当我想将数据从数据库导出到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";
}
/*错误*/
调用workbook.write()时;Workbook.Close();
已在执行
response.getOutputStream().flush();
response.getOutputStream().close();
所以抛出了异常,移除那些行就可以了。
最大的问题是,对于以下异常,代码片段可以正常工作。用户可以将文档保存在所需的位置。我想弄清楚为什么我会得到这个错误。
稍后,在同一个JSP中,我调用一个方法,该方法最终调用jasper-reports: HttpServletResponse对象“Response”被传递给以下方法,在调用时发生错误: 我已经查看了以下堆栈溢出帖子,仍然需要一些关于如何修复此问题的指导:
代码: 我的如下所示: 在JSP中,我只是给出了一个按钮,它给出了对话框。单击该按钮后,我将获得异常。 如何避免这一点?
我正在做关于CXF和Spring的培训,所以我写了一个非常简单的CXF演示,其中只有接口“HelloWorld”和它的实现者“HelloWorldWs”。 我想用Tomcat发布它。我编写了web.xml和applicationcontext.xml(Spring配置文件。虽然我可以发布WSDL。但是控制台列出了一个问题: 我没有使用或编写任何io函数,只是一个“sayhi”函数。我被难倒了。
我正在用Spring做一个项目,我有这个问题,我谷歌了错误信息,我找到了解决方案,甚至所有关于这个问题的帖子 有人能帮忙吗?