通过HttpResponse Java下载Zip文件 [英] Downloading Zip File through HttpResponse Java

查看:266
本文介绍了通过HttpResponse Java下载Zip文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我要从数据库(各种mimetypes)中获取blob的集合,并尝试将其压缩以供用户通过http响应下载.我可以进行下载,但是当我尝试打开下载的zip文件时,它会显示档案格式未知或已损坏."我已经尝试将以下代码与application/zip,application/octet-stream和application/x-zip压缩在一起,但是我开始假设问题出在如何添加文件上.我也在使用Java 7和Grails 2.2.4.

So I'm grabbing a collection of blobs from a database (various mimetypes) and trying to zip them up to be downloaded by users through an http response. I can get the download to happen, but when I try to open the downloaded zip file it says "The archive is either in unknown format or damaged." I've tried the following code with application/zip, application/octet-stream, and application/x-zip-compressed, but I'm starting to assume that the issue is in how I'm adding the files. I'm also using Java 7 and Grails 2.2.4.

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

Any help with this would be greatly appreciated. Thanks!

  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();

推荐答案

我在这里找到了答案:返回ZipOutputStream到浏览器

I found the answer here: Returning ZipOutputStream to browser

好的,所以最终对我有用的是转换

All right, so what ended up working for me was converting the

ZipOutputStream to a ByteArrayOutputStream and writing it to a response as a 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();

感谢所有提供帮助的人!

Thanks to everyone who helped!

这篇关于通过HttpResponse Java下载Zip文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆