gpt4 book ai didi

java - Playframework 2.5.0 : Unable to serve server-side generated zip file

转载 作者:行者123 更新时间:2023-12-02 02:55:56 25 4
gpt4 key购买 nike

我的 Playframework 2.5.0 应用程序必须提供临时创建的 zip 文件。我在/tmp 目录中创建并完成了 zip 文件,没有任何问题(我可以在这个地方打开它并提取其中包含的文件)。

但是发送给客户端的文件似乎被截断,无法打开。

String tempPath = "/tmp/" + label + ".zip";

File zipFile = new File(tempPath);
zipFile.deleteOnExit();
ZipOutputStream zos = null;

try {
zos = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFile)));

for (/* loops through files to zip */) {
InputStream is = methodToGetTheDocument();
ZipEntry zipEntry = new ZipEntry(document.getLabel());
zos.putNextEntry(zipEntry);
byte[] bytes = new byte[2048];
int count = is.read(bytes);
while (count > -1) {
zos.write(bytes, 0, count);
count = is.read(bytes);
}
is.close();
zos.closeEntry();
}
return ok(zipFile);
} catch (Exception e) {
return badRequest("Bad request");
} finally {
try {
zos.close();
} catch (Exception e) {
e.printStackTrace();
}
}

我返回一个经典的 Result 对象...问题是什么?

最佳答案

您必须使用 zos.close() “关闭”zip 文件在你面前return ok(zipFile)

关于java - Playframework 2.5.0 : Unable to serve server-side generated zip file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43071560/

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