-3

I'm trying to send multiple files at onces in my Response body. My issue is that i was not able to concat multiples Array List into one that i'm later able to re seperate into multiple files.

This is my code (that is not working) :

       List<PDDocument> documents =  splitter.split(PDDocument.load(documentData));
            ArrayList<byte[]> newDocuments = new ArrayList<>();
            for (PDDocument doc : documents)
            {
                ByteArrayOutputStream os = new ByteArrayOutputStream();
                doc.save(os);
                newDocuments.add(os.toByteArray());
                os.close();


            }
            t.sendResponseHeaders(200,newDocuments.toArray().length);;
            OutputStream responseBody = t.getResponseBody();
            responseBody.write(newDocuments.toArray());
            responseBody.close();

So my question is :

How to send back multiple files into a single http reponse using java 11 http server ?

Thank you !

UPDATE :

After fixing my code with the help of Joni i'm facing another issue : The Zip that is generated is corrupted :

This is the code :

   Splitter splitter = new Splitter();
            List<PDDocument> documents =  splitter.split(PDDocument.load(documentData));


            t.sendResponseHeaders(200, 0);
            t.getResponseHeaders().set("Content-Type", "application/zip");

            OutputStream responseBody = t.getResponseBody();
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ZipOutputStream zos = new ZipOutputStream(baos);


            int counter = 1;
            for (PDDocument doc : documents)
            {
                ZipEntry zipEntry = new ZipEntry("document" + counter);
                zos.putNextEntry(zipEntry);
                ByteArrayOutputStream docOs = new ByteArrayOutputStream();
                doc.save(docOs);
                docOs.close();
                zos.write(docOs.toByteArray());
                zos.closeEntry();
                zos.finish();
                zos.flush();

                counter++;
            }
            zos.close();
            baos.close();


            responseBody.write(baos.toByteArray());
            responseBody.flush();


            responseBody.close();
Michel Melhem
  • 551
  • 1
  • 8
  • 22
  • How do you want the client making the call differentiate the multiple arrays of bytes? What `Content-type` does/should the client expect the data to be? JSON? Multipart? Zip? Tar? What? – Andreas Aug 24 '20 at 12:22

1 Answers1

2

You cannot send multiple files in a HTTP response.

What you can do is put multiple files in one "compressed archive" file such as a ZIP file, and send that instead. For example:

        t.getResponseHeaders().set("Content-Type", "application/zip");
        t.sendResponseHeaders(200, 0);
        OutputStream responseBody = t.getResponseBody();
        ZipOutputStream zos = new ZipOutputStream(responseBody);
        int counter = 1;
        for (PDDocument doc : documents)
        {
            ZipEntry zipEntry = new ZipEntry("document"+counter);
            zos.putNextEntry(zipEntry);
            doc.save(zos);
            zos.closeEntry();
            counter++;
        }
        zos.close();
Joni
  • 108,737
  • 14
  • 143
  • 193
  • 1
    There is no need to call `closeEntry()`. --- You should also make sure to set the content type to `application/zip` in the response header. – Andreas Aug 24 '20 at 12:25
  • @Joni how would you get the final size of the zip file to send the final responseHeader ? – Michel Melhem Aug 24 '20 at 13:20
  • You can get the final size by writing the zip file into a `ByteArrayOutputStream` instead of writing it directly to the network. But you don't need the size to use `sendResponseHeaders`, you can set size 0 and the server will switch to using "chunked" transfer encoding which does not need a content length. – Joni Aug 24 '20 at 13:57
  • @Joni I followed your answer but the zip file that is generated is corrupted : i posted the new code in my updated post – Michel Melhem Aug 24 '20 at 14:53
  • I think you should post this as a new question on this site. You're having a completely different problem now. – Joni Aug 24 '20 at 15:00