通过 HttpResponse Java 下载 Zip 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33224123/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Downloading Zip File through HttpResponse Java
提问by Brewster
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.
因此,我从数据库(各种 mimetypes)中获取一组 blob,并尝试将它们压缩以供用户通过 http 响应下载。我可以进行下载,但是当我尝试打开下载的 zip 文件时,它显示“存档格式未知或已损坏”。我已经使用 application/zip、application/octet-stream 和 application/x-zip-compressed 尝试了以下代码,但我开始假设问题在于我如何添加文件。我也在使用 Java 7 和 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();
回答by Brewster
I found the answer here: Returning ZipOutputStream to browser
我在这里找到了答案:将 ZipOutputStream 返回到浏览器
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!
感谢所有帮助过的人!