错误:在读取文件时关闭流

时间:2015-12-02 13:27:35

标签: java io itext

我正在尝试修改现有PDF文件并在新浏览器上显示。但我得到“java.io.IOException:Stream Closed”异常,文件没有打开。只打开浏览器。 代码:

        response.setContentType("application/pdf");
        response.setHeader("Content-Disposition","inline; filename=ChallanStamped.pdf");
        PdfReader pdfReader = null;
        PdfStamper pdfStamper = null;
        OutputStream os = response.getOutputStream();
        FileInputStream in = new FileInputStream("G:/apache-tomcat-6.0.18/webapps/Programs/Job Application Site/WebContent/Documents/Challan1.pdf");
        try {
               ByteArrayOutputStream baos = new ByteArrayOutputStream();                   
               pdfReader = new PdfReader(in, null);
               pdfStamper = new PdfStamper(pdfReader, baos);
               for(int i=1; i<= pdfReader.getNumberOfPages(); i++){ 

                  PdfContentByte pdfcontent = pdfStamper.getUnderContent(i);                
                  pdfcontent.beginText();
                  ColumnText.showTextAligned(pdfcontent, Element.ALIGN_JUSTIFIED_ALL, "1234", 500, 428, 0); /*Do not change this values*/
                  ColumnText.showTextAligned(pdfcontent, Element.ALIGN_JUSTIFIED, "ABC", 500, 407, 0);      
                  ColumnText.showTextAligned(pdfcontent, Element.ALIGN_JUSTIFIED, "05/10/1983", 500, 386, 0);                    
                  pdfcontent.endText();            
            }   
                int len;
                while ((len = in.read()) != -1) {                       
                   os.write(len);
                }   
                os.flush();
                os.close();
           try {
                pdfStamper.close();
            } catch (DocumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
           pdfReader.close();

请建议我哪里出错了。

解决方案:将内容写入pdfclose pdfstamper然后OutputStream os = response.getOutputStream();。并将此输出流写入baos。然后冲洗os。

0 个答案:

没有答案