从java导出为pdf文件时,已为此响应调用getOutputStream()

从java导出为pdf文件时,已为此响应调用getOutputStream(),java,export-to-pdf,Java,Export To Pdf,我的java类中有以下代码部分 if(exportTo.equals("pdf")) { response.setHeader("Content-disposition", "attachment; filename=\"" + reportName + ".pdf\""); response.setContentType("application/pdf"); PdfWriter.getInstance(document,resp

我的java类中有以下代码部分

if(exportTo.equals("pdf"))
        {
        response.setHeader("Content-disposition", "attachment; filename=\"" + reportName + ".pdf\"");
        response.setContentType("application/pdf");
        PdfWriter.getInstance(document,response.getOutputStream());

        try {
            document.open();
            addTitlePage(document, reportName);

            Image image = Image.getInstance(path+"/"+"images/abi.png");
            image.setAbsolutePosition(40f, 770f);
            image.scaleAbsolute(70f, 50f);
            document.add(image);
            response.flushBuffer();

            //float[] colsWidth = {1.5f,3f,4f,4f,2f};

            List<Float> colsWidth = new ArrayList<Float>();
            int iterator = 1;
           while (iterator <= headerMap.size()) {
               if(iterator==1){
                   colsWidth.add(1.5f); 
               }else{
                colsWidth.add(3f); 
               }
                iterator++;
            }
           float[] floatArray = ArrayUtils.toPrimitive(colsWidth.toArray(new Float[0]), 0.0F);

           PdfPTable table = new PdfPTable(floatArray);
            table.setWidthPercentage(98);
            table.setHorizontalAlignment(Element.ALIGN_CENTER);

            PdfPCell c1 = new PdfPCell();
            for (Iterator it = headerMap.keySet().iterator(); it.hasNext();) {
                String headerName = (String) headerMap.get(it.next());
                c1 = new PdfPCell(new Phrase(headerName, headerFont));
                c1.setBackgroundColor(BaseColor.LIGHT_GRAY);
                table.addCell(c1);
            }
            table.setHeaderRows(1);
            table = custDAO.creadPDFTable(query, table);
            document.add(table);
            document.addAuthor(userViewModel.getUsername());
            document.addCreationDate();
            document.addCreator("POC");
            document.close();
            response.flushBuffer();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        }
我使用上述代码将图像添加到pdf文件(
itextpdflibrary

什么可以解决这个问题,请告诉我,因为我已经面临了很长一段时间


关于。

可能是一个解决方案:

尝试在尝试结束时放置所有文档内容:

try{
// create all object here, but don't add it to document yet.

// add all to document:
PdfWriter.getInstance(document,response.getOutputStream()); 
document.open();
addTitlePage(document, reportName);
document.add(image);
document.add(table);
document.addAuthor(userViewModel.getUsername());
document.addCreationDate();
document.addCreator("POC");
document.close();
response.flushBuffer();

} catch (Exception ex) {
  ex.printStackTrace();
}

没有其他方法可以避免这个异常

我有一个单独的方法将图像添加到文档中,并将其称为,这将解决我的问题

谢谢你的帮助

Image image = Image.getInstance(path+"/"+"images/abi.png");
                image.setAbsolutePosition(40f, 770f);
                image.scaleAbsolute(70f, 50f);
                document.add(image);
                response.flushBuffer();

如果删除第一个
response.flushBuffer()?哪一行?您能给出堆栈跟踪吗?
Image=Image.getInstance(路径+“/”+“images/abi.png”)
是它给出异常的一行,但当我看到控制台时,它说file not found Exction给出了文件的完整路径,如
D:\Reports\images\abi.png
,但它不是一个独立的应用程序,它是一个web应用程序
Image image = Image.getInstance(path+"/"+"images/abi.png");
                image.setAbsolutePosition(40f, 770f);
                image.scaleAbsolute(70f, 50f);
                document.add(image);
                response.flushBuffer();