簡體   English   中英

刪除或不返回 java 中的 BufferedOutputStream 文件

[英]remove or not return BufferedOutputStream file in java

返回 java 方法時,我不想下載BufferedOutputStream

我的代碼:

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();

我的問題是當發票有編號時,它會生成發票並下載到壓縮的 zip 文件中。 但是當它沒有號碼時,我不想下載 zip。 但仍會下載 zip 文件,但其中沒有空文件。

如果沒有生成 pdf,我不想下載 zip 文件。

任何幫助...

一旦您開始生成 ZIP 並將其寫入響應 output stream,就沒有回頭路了。 只需打開 stream 就會導致響應“提交”......這意味着您不能再更改響應代碼或標頭。

基本上,您需要在開始生成響應之前檢查是否有任何發票。 那么它應該只是重組現有代碼的問題。

就像是.....

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();

我已經修復了一些不好的風格。 看看你能不能發現變化...

還有一個我沒有解決的問題:即在這段代碼中打開的各種資源應該使用try with resources來管理。 但是,這可能不是必需的,因為看起來資源都基於請求 output stream。 這將由 servlet 基礎結構自動關閉。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM