[英]Generate Zip File - download PDF from Url and Generate Zip to Download in Browser - Broken PDF
[英]unable to Generate a ZIP file on the fly and send it to the user’s browser for download
我有音頻剪輯的URL。 我想從它們的URL下載所有這些剪輯,並將所有這些剪輯放在單個zip文件中。 如果我在代碼中指定了硬編碼路徑,則可以使用所有剪輯在zip格式的指定位置成功下載該路徑。 但是,我想生成一個彈出窗口,提示用戶指定下載該zip文件的路徑。 這是我的代碼:
ByteArrayOutputStream baos = new ByteArrayOutputStream()
ZipOutputStream zipFile = new ZipOutputStream(baos)
for (int i = 0; i < clipsCount; i++) {
String fileName = getSessionId() + ".mp4";
try {
//Opening Connection to the Downloading Link
URLConnection conn = new URL(sessionToDownload.getConvertedLinkURL()).openConnection();
conn.setRequestProperty("Authorization", "Basic " + encodedString);
//Adding File in the Zipped Resource
def file1Entry = new ZipEntry(fileName);
zipFile.putNextEntry(file1Entry);
//Getting Required File
zipFile << conn.getInputStream();
//Closing Current Zip Entry
zipFile.closeEntry();
println "Session to Download: " + listofId.getAt(i);
} catch (Exception ex) {
println "Exception while Downloading Session: "+ex.getMessage();
}
}
zipFile.close()
response.setHeader("Content-disposition", "attachment; filename=\"/download.zip\" ")
response.setContentType("application/zip");
response.outputStream << baos
response.outputStream.flush()
webRequest.renderView = false;
因此,在Loop中,它會針對每個剪輯進行迭代,下載並使其成為zip文件的一部分。
如果我做錯了什么,或者可以通過其他方法實現,請給我指導。 感謝您的時間,考慮和指導。
您的操作應該確實有效。 如果瀏覽器在響應中看到帶有"attachment; filename=aaa.zip"
的值的"Content-disposition"
-header,則會自動打開瀏覽器的“ Save as..
對話框。
嘗試刪除引號:
response.setHeader 'Content-disposition', 'attachment; filename=download.zip'
我已經調查了此問題,實際上是由於此處描述的問題而產生的:將多個選定的項目從控制器發送到視圖
還有一個問題,它不允許它出現在瀏覽器端。 這對我有用:
//Setting Response Required Parameters
response.setContentType('APPLICATION/OCTET-STREAM')
response.setHeader('Content-Disposition', 'Attachment;Filename="example.zip"')
InputStream contentStream = null;
ZipOutputStream zip = new ZipOutputStream(response.outputStream);
for (int i = 0; i < listofId.length; i++) {
def sessionToDownload = Sessions.get(listofId.getAt(i));
if (sessionsToDownload != null) {
String fileName = sessionToDownload.getSessionId() + ".mp4";
try {
//Opening Connection to the Downloading Link
URLConnection conn = new URL(sessionToDownload.getConvertedLinkURL()).openConnection();
conn.setRequestProperty("Authorization", "Basic " + encodedString);
//Getting file contents
contentStream = conn.getInputStream();
//Adding File in the Zipped Resource
def file1Entry = new ZipEntry(fileName);
zip.putNextEntry(file1Entry);
zip.write(IOUtils.toByteArray(contentStream));
println "Session to Download: " + fileName;
} catch (Exception ex) {
println "Exception while Downloading Session: " + ex.getMessage();
}
}
}
zip.close();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.