收集输入流(pdf' s)并使用PDFMergerUtility合并为单个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();
一切都按预期工作。但是,当第二次执行时,我收到以下错误:流关闭
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);
源值未清除推进
示例:第一次执行(部署后)计数将为2(源列表),因此生成的文档没有任何问题
然后我尝试第二次运行,假设当前总页数为3,现在源计数变为5,这是错误的
所以请帮助我如何在创建文档后清除sources
列表
答案 0 :(得分:0)
您需要在每次迭代后清除列表,在最后一个块中添加此行,
sources.clear();
希望这会有所帮助