英文:
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<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();
答案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("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();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论