【发布时间】:2014-07-23 10:29:58
【问题描述】:
我有音频剪辑的 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 文件的一部分。
如果我做错了什么,请指导我,或者我可以通过其他方式实现这一点。 感谢您的时间、考虑和指导。
【问题讨论】:
-
您是否使用相同的文件名保存所有文件?
-
不,该 zip 文件中的每个文件都有不同的名称
-
您的代码正好相反。您使用相同的
fileName变量命名所有 zipEntries -
如果你仔细看,我会在循环的每次迭代中更改“文件名”变量
-
如果
getSessionId()做了我认为的那样,它不仅对整个循环,而且对整个会话都应该具有相同的值:)
标签: java grails response zipfile savefiledialog