因此,我從數據庫(各種mimetypes)抓取了一組blob,並試圖將它們壓縮成由用戶通過http響應下載。我可以讓下載發生,但是當我嘗試打開下載的zip文件時,它會顯示「存檔文件格式未知或已損壞。」我嘗試了下面的代碼,使用application/zip,application/octet-stream和application/x-zip-compressed,但我開始認爲問題在於我如何添加文件。我也在使用Java 7和Grails 2.2.4。通過HttpResponse下載Zip文件Java
任何幫助,這將不勝感激。謝謝!
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感謝您回覆Shaun。我直接將它下載到我的電腦上,我可以毫無問題地打開它。我想這意味着問題在於我將其推入響應的方式? – Brewster