【问题标题】:Playframework 2.5.0 : Unable to serve server-side generated zip filePlayframework 2.5.0:无法提供服务器端生成的 zip 文件
【发布时间】:2017-03-28 13:56:10
【问题描述】:

我的 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 对象...有什么问题?

【问题讨论】:

  • 如果您不更改 zip 文件,为什么不只是 return ok(new java.io.File("/path/to/7file.zip"));

标签: java playframework playframework-2.5


【解决方案1】:

您必须先使用zos.close()“关闭”压缩文件,然后再使用return ok(zipFile)

【讨论】:

    猜你喜欢
    • 2016-10-23
    • 2020-01-03
    • 1970-01-01
    • 2021-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-27
    相关资源
    最近更新 更多