i would like not to download the BufferedOutputStream
when return java method.
my code:
FacesContext ctx = FacesContext.getCurrentInstance(); HttpServletResponse response = (HttpServletResponse) ctx.getExternalContext().getResponse(); response.setHeader("Content-Disposition", "attachment; filename="" + "Invoice.zip";"); BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream()); ZipOutputStream zos = new ZipOutputStream(bos); for(SalesEInvObject InvoiceObj : this.InvoiceTable){ // MAIN FOR-LOOP STARTS if (InvoiceObj.getInvoiceNo() != null) { javax.servlet.http.HttpSession httpSession =(javax.servlet.http.HttpSession) ctx.getExternalContext().getSession(false); httpSession.setAttribute(BaseHttpServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, reportOutput.getInternalReportObject()); byte[] bytes = reportOutput.getReportOutputBytes(); int length = ((bytes == null) ? 0 : bytes.length); response.setContentLength(length*tableSize); final ZipEntry ze = new ZipEntry(reportOutputFileName+".pdf"); zos.putNextEntry(ze); zos.write(bytes, 0, bytes.length); zos.closeEntry(); }else { return null; } }//LOOP ENDS zos.close(); ctx.responseComplete();
my problem is when the invoices has Number it generates invoice and download in compressed zip file. but when it has no Number i dont want to download zip. but still zip file downloads but with empty no file in it.
if no pdf generated i dont want to download zip file.
any help…
Advertisement
Answer
Once you have started generating and writing the ZIP to the response output stream, there is no turning back. Just opening the stream causes the response to “commit” … meaning that you can no longer change the response code or headers.
Basically, you need to check if there are any invoices before you start generating the response. Then it should just be a matter of reorganizing the existing code.
Something like …..
boolean hasInvoices = false; for (SalesEInvObject invoiceObj : this.InvoiceTable) { if (invoiceObj.getInvoiceNo() != null) { hasInvoices = true; break; } } FacesContext ctx = FacesContext.getCurrentInstance(); HttpServletResponse response = (HttpServletResponse) ctx.getExternalContext().getResponse(); if (hasInvoices) { response.setHeader("Content-Disposition", "attachment; filename="" + "Invoice.zip";"); BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream()); ZipOutputStream zos = new ZipOutputStream(bos); for (SalesEInvObject invoiceObj : this.InvoiceTable) { if (invoiceObj.getInvoiceNo() != null) { javax.servlet.http.HttpSession httpSession = (javax.servlet.http.HttpSession) ctx.getExternalContext() .getSession(false); httpSession.setAttribute( BaseHttpServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, reportOutput.getInternalReportObject()); byte[] bytes = reportOutput.getReportOutputBytes(); int length = ((bytes == null) ? 0 : bytes.length); response.setContentLength(length * tableSize); final ZipEntry ze = new ZipEntry(reportOutputFileName + ".pdf"); zos.putNextEntry(ze); zos.write(bytes, 0, bytes.length); zos.closeEntry(); } } zos.close(); } else { // do you want to set a response code or something? } ctx.responseComplete();
I have fixed some bad style. See if you can spot the changes …
There is another problem that I haven’t addressed: namely that the various resources that are opened in this code ought to be managed using try with resources. However, it may not be necessary since it looks like the resources are all based on with the request output stream. That will be closed automatically by the servlet infrastructure.