【问题标题】:Downloading Zip File through HttpResponse Java通过 HttpResponse Java 下载 Zip 文件
【发布时间】:2016-01-18 08:57:15
【问题描述】:

因此,我从数据库(各种 mimetypes)中获取 blob 集合,并尝试将它们压缩以供用户通过 http 响应下载。我可以进行下载,但是当我尝试打开下载的 zip 文件时,它显示“存档格式未知或已损坏”。我已经使用 application/zip、application/octet-stream 和 application/x-zip-compressed 尝试了以下代码,但我开始认为问题在于我如何添加文件。我也在使用 Java 7 和 Grails 2.2.4。

对此的任何帮助将不胜感激。谢谢!

  final ZipOutputStream out = new ZipOutputStream(new FileOutputStream("test.zip"));


        for (Long id : ids){

            Object[] stream = inlineSamplesDataProvider.getAttachmentStream(id);


            if (stream) {

                String fileName = stream[0]
                String mimeType = (String) stream[1];
                InputStream inputStream = stream[2]
                byte[] byteStream = inputStream.getBytes();

                ZipEntry zipEntry = new ZipEntry(fileName)
                out.putNextEntry(zipEntry);
                out.write(byteStream, 0, byteStream.length);
                out.closeEntry();
            }
        }

        out.close();
        response.setHeader("Content-Disposition", "attachment; filename=\"" + "test.zip" + "\"");
        response.setHeader("Content-Type", "application/zip");
        response.outputStream << out;
        response.outputstream.flush();

【问题讨论】:

  • 这似乎或多或少没问题,至少您编写文件的方式应该可以工作。在通过网络发送之前,您是否尝试过运行此代码并让它将文件保存到磁盘以查看是否可以打开它。
  • @ShaunStone 感谢您的回复,肖恩。我直接把它下载到我的电脑上,我可以毫无问题地打开它。我想这意味着问题在于我将其推送到响应中的方式?

标签: java zip mime-types httpresponse


【解决方案1】:

我在这里找到了答案:Returning ZipOutputStream to browser

好吧,最终对我有用的是将 ZipOutputStream 转换为 ByteArrayOutputStream 并将其作为 byte[] 写入响应:

ByteArrayOutputStream baos = new ByteArrayOutputStream();
final ZipOutputStream out = new ZipOutputStream(baos);

Calendar cal = Calendar.getInstance();
String date = new SimpleDateFormat("MMM-dd").format(cal.getTime());

final String zipName = "COA_Images-" + date + ".zip";

for (Long id: ids) {

    Object[] stream = inlineSamplesDataProvider.getAttachmentStream(id);

    if (stream) {

        String fileName = stream[0];
        String mimeType = (String) stream[1];
        InputStream inputStream = stream[2];
        byte[] byteStream = inputStream.getBytes();

        ZipEntry zipEntry = new ZipEntry(fileName)
        out.putNextEntry(zipEntry);
        out.write(byteStream, 0, byteStream.length);
        out.closeEntry();
    }
}

out.close();

response.setHeader("Content-Disposition", "attachment; filename=\"" + zipName + "\"");
response.setHeader("Content-Type", "application/zip");
response.getOutputStream().write(baos.toByteArray());
response.flushBuffer();
baos.close();

感谢所有帮助过的人!

【讨论】:

  • 您可以将自己的答案标记为正确答案,以获得徽章:)
  • 感谢您的回答,让我省了很多头痛!
  • 我使用了一组从 db blob 读取的字节并制作了 zip 文件。 zip文件下载但无法打开。任何人都可以帮忙吗? dataBytes = doc.getPdfData(); ZipEntry 条目 = 新 ZipEntry(pdfFileName); entry.setSize(dataBytes.length); zos.putNextEntry(entry); zos.write(dataBytes, 0, dataBytes.length); zos.closeEntry(); response.setContentType("应用程序/zip"); response.addHeader("Content-Disposition", "attachment;filename=" + zipName); response.setContentLength(baos.toByteArray().length); stream.write(baos.toByteArray());
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-02-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-01
相关资源
最近更新 更多