Skip to content

Commit 0b68552

Browse files
committed
Mejoras JasperReportUtil
1 parent da6af7f commit 0b68552

File tree

1 file changed

+27
-27
lines changed

1 file changed

+27
-27
lines changed

web/src/main/java/org/javabeanstack/web/util/JasperReportUtil.java

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -180,24 +180,24 @@ public void print(String reportName, Map<String, Object> parameters,
180180
HttpServletResponse httpServletResponse = (HttpServletResponse) facesCtx.getExternalContext().getResponse();
181181
httpServletResponse.setContentType("text/html;charset=UTF-8");
182182
httpServletResponse.addHeader("Content-disposition", "attachment; filename=" + reporte.replaceAll(".jasper", "") + ".html");
183-
ServletOutputStream servletOutputStream = httpServletResponse.getOutputStream();
184-
SimpleHtmlExporterOutput simpleHtmlExporterOutput = new SimpleHtmlExporterOutput(servletOutputStream);
185-
exporterHTML.setExporterOutput(simpleHtmlExporterOutput);
186-
exporterHTML.exportReport();
187-
servletOutputStream.flush();
188-
servletOutputStream.close();
183+
try (ServletOutputStream servletOutputStream = httpServletResponse.getOutputStream()) {
184+
SimpleHtmlExporterOutput simpleHtmlExporterOutput = new SimpleHtmlExporterOutput(servletOutputStream);
185+
exporterHTML.setExporterOutput(simpleHtmlExporterOutput);
186+
exporterHTML.exportReport();
187+
servletOutputStream.flush();
188+
}
189189
FacesContext.getCurrentInstance().responseComplete();
190190
} else if ("doc".equals(parameters.get("device"))) {
191191
JRDocxExporter exporter = new JRDocxExporter();
192192
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
193193
HttpServletResponse httpServletResponse = (HttpServletResponse) facesCtx.getExternalContext().getResponse();
194194
httpServletResponse.setContentType("application/nd.openxmlformats-officedocument.wordprocessingml.document");
195195
httpServletResponse.setHeader("Content-Disposition", "attachment;filename= " + reporte.replaceAll(".jasper", "") + ".docx");
196-
OutputStream outputStream = httpServletResponse.getOutputStream();
197-
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(outputStream));
198-
exporter.exportReport();
199-
outputStream.flush();
200-
outputStream.close();
196+
try (OutputStream outputStream = httpServletResponse.getOutputStream()) {
197+
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(outputStream));
198+
exporter.exportReport();
199+
outputStream.flush();
200+
}
201201
FacesContext.getCurrentInstance().responseComplete();
202202
} else if ("pdf".equals(parameters.get("device"))) {
203203
String target;
@@ -222,22 +222,22 @@ public void print(String reportName, Map<String, Object> parameters,
222222
// Extensión de archivo actualizada a .xlsx
223223
httpServletResponse.setHeader("Content-Disposition", "attachment;filename=" + reporte.replaceAll(".jasper", "") + ".xlsx");
224224

225-
OutputStream outputStream = httpServletResponse.getOutputStream();
226-
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(outputStream));
227-
228-
// Usamos la configuración específica para XLSX
229-
SimpleXlsxReportConfiguration configuration = new SimpleXlsxReportConfiguration();
230-
configuration.setOnePagePerSheet(false);
231-
configuration.setDetectCellType(true);
232-
configuration.setCollapseRowSpan(false);
233-
configuration.setWhitePageBackground(false);
234-
configuration.setRemoveEmptySpaceBetweenRows(true);
235-
236-
exporter.setConfiguration(configuration);
237-
exporter.exportReport();
238-
239-
outputStream.flush();
240-
outputStream.close();
225+
try (OutputStream outputStream = httpServletResponse.getOutputStream()) {
226+
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(outputStream));
227+
228+
// Usamos la configuración específica para XLSX
229+
SimpleXlsxReportConfiguration configuration = new SimpleXlsxReportConfiguration();
230+
configuration.setOnePagePerSheet(false);
231+
configuration.setDetectCellType(true);
232+
configuration.setCollapseRowSpan(false);
233+
configuration.setWhitePageBackground(false);
234+
configuration.setRemoveEmptySpaceBetweenRows(true);
235+
236+
exporter.setConfiguration(configuration);
237+
exporter.exportReport();
238+
239+
outputStream.flush();
240+
}
241241
FacesContext.getCurrentInstance().responseComplete();
242242
}
243243
} else {

0 commit comments

Comments
 (0)