I have a webservice which returns pdf invoices. The client code is using HttpURLConnection to consume the service. It reads the invoice in InputStream and then write to the OutputStream. All of this is working. Now there is a requirement to select multiple invoices and press the download button. It should concatenate the selected invoices and open as one browsable document.
I have tried using the SequenceInputStream as below but it always open the last selected invoice. Here is my code
for (int i=0; i < docIds.length; i++) {
URL url = new URL(baseurl + "/invoices/" + docIds[i]);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Authorization", Application.get("documentum", "Authorization"));
conn.setRequestProperty("Accept", "application/pdf");
conn.setConnectTimeout(20000);
conn.setReadTimeout(20000);
conn.connect();
if (!conn.getContentType().equalsIgnoreCase("application/pdf")) {
System.out.println("FAILED.\n[Sorry. This is not a PDF.]");
return false;
} else {
response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "filename=fbau-doc.pdf");
outputStream = response.getOutputStream();
InputStream in = conn.getInputStream();
streams.add(in);
}
SequenceInputStream sequenceInputStream = new SequenceInputStream(streams.elements());
// clean up
IOUtils.copy(sequenceInputStream, outputStream);
outputStream.close();