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

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

put multiple byte[] into a single reponse body

问题

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

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

  1. List<PDDocument> documents = splitter.split(PDDocument.load(documentData));
  2. ArrayList<byte[]> newDocuments = new ArrayList<>();
  3. for (PDDocument doc : documents) {
  4. ByteArrayOutputStream os = new ByteArrayOutputStream();
  5. doc.save(os);
  6. newDocuments.add(os.toByteArray());
  7. os.close();
  8. }
  9. t.sendResponseHeaders(200, newDocuments.toArray().length);
  10. OutputStream responseBody = t.getResponseBody();
  11. responseBody.write(newDocuments.toArray());
  12. responseBody.close();

所以我的问题是:

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

谢谢!

更新:

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

这是代码:

  1. Splitter splitter = new Splitter();
  2. List<PDDocument> documents = splitter.split(PDDocument.load(documentData));
  3. t.sendResponseHeaders(200, 0);
  4. t.getResponseHeaders().set("Content-Type", "application/zip");
  5. OutputStream responseBody = t.getResponseBody();
  6. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  7. ZipOutputStream zos = new ZipOutputStream(baos);
  8. int counter = 1;
  9. for (PDDocument doc : documents) {
  10. ZipEntry zipEntry = new ZipEntry("document" + counter);
  11. zos.putNextEntry(zipEntry);
  12. ByteArrayOutputStream docOs = new ByteArrayOutputStream();
  13. doc.save(docOs);
  14. docOs.close();
  15. zos.write(docOs.toByteArray());
  16. zos.closeEntry();
  17. zos.finish();
  18. zos.flush();
  19. counter++;
  20. }
  21. zos.close();
  22. baos.close();
  23. responseBody.write(baos.toByteArray());
  24. responseBody.flush();
  25. 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) :

  1. List&lt;PDDocument&gt; documents = splitter.split(PDDocument.load(documentData));
  2. ArrayList&lt;byte[]&gt; newDocuments = new ArrayList&lt;&gt;();
  3. for (PDDocument doc : documents)
  4. {
  5. ByteArrayOutputStream os = new ByteArrayOutputStream();
  6. doc.save(os);
  7. newDocuments.add(os.toByteArray());
  8. os.close();
  9. }
  10. t.sendResponseHeaders(200,newDocuments.toArray().length);;
  11. OutputStream responseBody = t.getResponseBody();
  12. responseBody.write(newDocuments.toArray());
  13. 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 :

  1. Splitter splitter = new Splitter();
  2. List&lt;PDDocument&gt; documents = splitter.split(PDDocument.load(documentData));
  3. t.sendResponseHeaders(200, 0);
  4. t.getResponseHeaders().set(&quot;Content-Type&quot;, &quot;application/zip&quot;);
  5. OutputStream responseBody = t.getResponseBody();
  6. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  7. ZipOutputStream zos = new ZipOutputStream(baos);
  8. int counter = 1;
  9. for (PDDocument doc : documents)
  10. {
  11. ZipEntry zipEntry = new ZipEntry(&quot;document&quot; + counter);
  12. zos.putNextEntry(zipEntry);
  13. ByteArrayOutputStream docOs = new ByteArrayOutputStream();
  14. doc.save(docOs);
  15. docOs.close();
  16. zos.write(docOs.toByteArray());
  17. zos.closeEntry();
  18. zos.finish();
  19. zos.flush();
  20. counter++;
  21. }
  22. zos.close();
  23. baos.close();
  24. responseBody.write(baos.toByteArray());
  25. responseBody.flush();
  26. responseBody.close();

答案1

得分: 2

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

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

  1. t.getResponseHeaders().set("Content-Type", "application/zip");
  2. t.sendResponseHeaders(200, 0);
  3. OutputStream responseBody = t.getResponseBody();
  4. ZipOutputStream zos = new ZipOutputStream(responseBody);
  5. int counter = 1;
  6. for (PDDocument doc : documents)
  7. {
  8. ZipEntry zipEntry = new ZipEntry("document" + counter);
  9. zos.putNextEntry(zipEntry);
  10. doc.save(zos);
  11. zos.closeEntry();
  12. counter++;
  13. }
  14. 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:

  1. t.getResponseHeaders().set(&quot;Content-Type&quot;, &quot;application/zip&quot;);
  2. t.sendResponseHeaders(200, 0);
  3. OutputStream responseBody = t.getResponseBody();
  4. ZipOutputStream zos = new ZipOutputStream(responseBody);
  5. int counter = 1;
  6. for (PDDocument doc : documents)
  7. {
  8. ZipEntry zipEntry = new ZipEntry(&quot;document&quot;+counter);
  9. zos.putNextEntry(zipEntry);
  10. doc.save(zos);
  11. zos.closeEntry();
  12. counter++;
  13. }
  14. 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:

确定