[英]Playframework 2.5.0 : Unable to serve server-side generated zip file
我的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對象...是什么問題?
return ok(zipFile)
之前,必須使用zos.close()
“關閉” zip文件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.