gpt4 book ai didi

java - 将生成的 Zip 的 Zip 提供给客户端

转载 作者:行者123 更新时间:2023-12-02 00:44:15 27 4
gpt4 key购买 nike

我正在生成一个 ZIP 文件,其中包含一堆自动生成的 XML 文件。最近需求发生了变化,现在我必须生成几次 ZIP(带有 XML 数据的变体)并将它们直接提供给客户端,而不使用服务器中的实际文件。这就是我正在做的事情:

// [... servlet handling ... ]
response.setContentType("application/zip");
response.setHeader("Content-Disposition", "attachment;filename=cierresZ_a_tangonet" + java.time.LocalDate.now() + ".zip");
// stream straight to client
ServletOutputStream out = response.getOutputStream();
ZipOutputStream zipped_out = new ZipOutputStream(out);
for( each data block from db ){
//CREATION AND PROCESSING OF XML FILES AS ZIP ENTRIES
byte[] xmlBinData = xmlData.toString().getBytes();
zipped_out.write(xmlBinData, 0, xmlBinData.length);
zipped_out.flush();
}
zipped_out.finish();
out.close();
}

尝试这样做但出现错误:

// [... servlet handling ... ]
response.setContentType("application/zip");
response.setHeader("Content-Disposition", "attachment;filename=cierresZ_a_tangonet" + java.time.LocalDate.now() + ".zip");
// stream straight to client
ServletOutputStream out = response.getOutputStream();
for( each zip needed ){
ZipOutputStream zipped_out = new ZipOutputStream(out);
for( each data block from db ){
//CREATION AND PROCESSING OF XML FILES AS ZIP ENTRIES
byte[] xmlBinData = xmlData.toString().getBytes();
zipped_out.write(xmlBinData, 0, xmlBinData.length);
zipped_out.flush();
}
zipped_out.finish();
}
out.close();
}

编辑:对其进行了一些细微的更改,但给出了相同的错误

// [... servlet handling ... ]
response.setContentType("application/zip");
response.setHeader("Content-Disposition", "attachment;filename=cierresZ_a_tangonet" + java.time.LocalDate.now() + ".zip");
// stream straight to client
ServletOutputStream out = response.getOutputStream();
ZipOutputStream zipped_outs = new ZipOutputStream(out);
for( each zip needed ){
//creates new zip inside big one
ZipEntry zipFile = new ZipEntry(salaActual + ".zip");
zipped_outs.putNextEntry(zipFile);

//opens stream for this new zip
ZipOutputStream zipped_out = new ZipOutputStream(zipped_outs);
for( each data block from db ){
//CREATION AND PROCESSING OF XML FILES AS ZIP ENTRIES
byte[] xmlBinData = xmlData.toString().getBytes();
zipped_out.write(xmlBinData, 0, xmlBinData.length);
zipped_out.flush();
}
//completes zip and closes it then goes for the next one
byte[] zipBinData = zipFile.toString().getBytes();
zipped_outs.write(zipBinData, 0, zipBinData.length);
zipped_outs.flush();
}
//closes the big zip filled with zips and returns
zipped_outs.finish();
out.close();
}

最佳答案

您需要finish()flush()所有内部ZipOutputStream外部ZipOutputStream。

基本上:

response.setContentType("application/zip");
response.setHeader("Content-Disposition", ...);
ZipOutputStream mainZip = new ZipOutputStream(response.getOutputStream());
for (each file to download) {
ZipEntry zipEntry = new ZipEntry(fileName);
mainZip.putNextEntry(zipEntry);
if (file data available) {
// write file data to mainZip here
} else {
ZipOutputStream subZip = new ZipOutputStream(mainZip);
for (each subfile to download) {
ZipEntry zipEntry = new ZipEntry(subFileName);
subZip.putNextEntry(zipEntry);
// write subfile data to subZip here
}
subZip.finish();
subZip.flush(); // do not close
}
}
mainZip.finish();
mainZip.close(); // flushes for you

请注意 mainZipsubZip 如何获取 zip 条目,并且都完成。您的代码中缺少这一点。

关于java - 将生成的 Zip 的 Zip 提供给客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57911777/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com