【问题标题】:IO Exception Stream Closed using PDFMergerUtility使用 PDFMergerUtility 关闭 IO 异常流
【发布时间】:2017-01-05 10:16:06
【问题描述】:

使用 PDFMergerUtility 收集输入流(pdf)并合并为单个 pdf 文档,下面是我的代码(带有 glassfish 服务器的 servlet)

static HttpsURLConnection connectionGet;
static List<InputStream> sources = new ArrayList<>();
Integer totalPageCount = Integer.parseInt(pageCountValue);
try {
    for (int i = 1; i <= totalPageCount; i++) {
        String inputValue = countryCode + "/" + kindCode;
        String pdfUrl = "abc.org/rest-services/published-data/images/" + inputValue + "/fullimage.pdf?Range=" + i;
        URL pdfDownload = new URL(pdfUrl);
        connectionGet = (HttpsURLConnection) pdfDownload.openConnection();
        String authorizationHeader1 = "Bearer " + getToken;
        connectionGet.setRequestProperty("Authorization", authorizationHeader1);
        connectionGet.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        connectionGet.setRequestMethod("GET");
        int pageResponseCode = connectionGet.getResponseCode();
        if (pageResponseCode != 404) {
            sources.add(connectionGet.getInputStream());
        }
    }  
    PDFMergerUtility pdfMerger = new PDFMergerUtility();
    pdfMerger.addSources(sources);
    String pdfFileName = countryCode+kindCode+".pdf";
    String contextPath = request.getServletContext().getRealPath("/");
    pdfMerger.setDestinationFileName(contextPath+pdfFileName);
    try {                            
        pdfMerger.mergeDocuments();
    } catch (COSVisitorException ex) {
       Logger.getLogger(PDFDownloadResult.class.getName()).log(Level.SEVERE, null, ex);
    }
    out.println("Document Downloaded Successfully in below server Path" + "<p><b>" +contextPath + pdfFileName+ "</b>");
    } catch (IOException e) {
        } finally {
                // cleanup
                    sources.stream().forEach((source) -> {
                    IOUtils.closeQuietly(source);
                });
        }
    connectionGet.disconnect();

一切都按预期进行。但是,当第二次执行时,我收到以下错误:Stream closed

Severe:   java.io.IOException: stream is closed
    at sun.net.www.protocol.http.HttpURLConnection$HttpInputStream.ensureOpen(HttpURLConnection.java:3295)
    at sun.net.www.protocol.http.HttpURLConnection$HttpInputStream.read(HttpURLConnection.java:3320)
    at java.io.BufferedInputStream.fill(BufferedInputStream.java:246)
    at java.io.BufferedInputStream.read(BufferedInputStream.java:265)
    at java.io.FilterInputStream.read(FilterInputStream.java:83)
    at java.io.PushbackInputStream.read(PushbackInputStream.java:139)
    at org.apache.pdfbox.io.PushBackInputStream.read(PushBackInputStream.java:90)
    at org.apache.pdfbox.io.PushBackInputStream.peek(PushBackInputStream.java:68)
    at org.apache.pdfbox.io.PushBackInputStream.isEOF(PushBackInputStream.java:156)
    at org.apache.pdfbox.pdfparser.BaseParser.readLine(BaseParser.java:1517)
    at org.apache.pdfbox.pdfparser.PDFParser.parseHeader(PDFParser.java:360)
    at org.apache.pdfbox.pdfparser.PDFParser.parse(PDFParser.java:186)
    at org.apache.pdfbox.pdmodel.PDDocument.load(PDDocument.java:1235)
    at org.apache.pdfbox.pdmodel.PDDocument.load(PDDocument.java:1202)
    at org.apache.pdfbox.util.PDFMergerUtility.mergeDocuments(PDFMergerUtility.java:235)
    at org.apache.pdfbox.util.PDFMergerUtility.mergeDocuments(PDFMergerUtility.java:192)

已编辑:问题是 IOUtils.closeQuietly(source); 源值未清除 propelry

示例:第一次执行(部署后)计数将为 2(来源列表),因此生成的文档没有任何问题

然后我尝试第二次运行,假设当前总页数为 3,现在源数变为 5,这是错误的

所以请帮助我在创建文档后如何清除sources 列表

【问题讨论】:

  • 第二次执行是指程序第二次运行还是在执行过程中第二次作为方法调用?
  • @karthi:这是 Web 应用程序第一次执行它的罚款,下一次产生 strem 关闭异常
  • 你试过不带disconnect()函数吗?
  • @Karthi 你的意思是 bufferedrader close() / disconnect
  • 只需删除此行即可尝试 connectionGet.disconnect();

标签: java servlets glassfish httpurlconnection ioexception


【解决方案1】:

您需要在每次迭代后清除列表,将此行添加到您的最终块中,
sources.clear();
希望这会有所帮助 .

【讨论】:

    猜你喜欢
    • 2014-05-18
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 2011-01-07
    • 2013-03-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多