将多个 byte[] 放入单个响应主体中。

huangapple go评论98阅读模式
英文:

put multiple byte[] into a single reponse body

问题

我试图在响应主体中一次性发送多个文件。我的问题是我无法将多个数组列表连接成一个数组列表,然后稍后能够将其重新分离为多个文件。

这是我的代码(不起作用):

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();

所以我的问题是:

如何使用Java 11 HTTP服务器将多个文件发送回单个HTTP响应?

谢谢!

更新:

在借助Joni修复了我的代码后,我面临另一个问题:
生成的Zip文件损坏:

这是代码:

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();
英文:

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&lt;PDDocument&gt; documents =  splitter.split(PDDocument.load(documentData));
            ArrayList&lt;byte[]&gt; newDocuments = new ArrayList&lt;&gt;();
            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&lt;PDDocument&gt; documents =  splitter.split(PDDocument.load(documentData));


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

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


            int counter = 1;
            for (PDDocument doc : documents)
            {
                ZipEntry zipEntry = new ZipEntry(&quot;document&quot; + 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();

答案1

得分: 2

你不能在HTTP响应中发送多个文件。

您可以将多个文件放入一个“压缩存档”文件中,例如ZIP文件,然后发送该文件。例如:

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();
英文:

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(&quot;Content-Type&quot;, &quot;application/zip&quot;);
        t.sendResponseHeaders(200, 0);
        OutputStream responseBody = t.getResponseBody();
        ZipOutputStream zos = new ZipOutputStream(responseBody);
        int counter = 1;
        for (PDDocument doc : documents)
        {
            ZipEntry zipEntry = new ZipEntry(&quot;document&quot;+counter);
            zos.putNextEntry(zipEntry);
            doc.save(zos);
            zos.closeEntry();
            counter++;
        }
        zos.close();

huangapple
  • 本文由 发表于 2020年8月24日 20:11:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/63560805.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定