Java 无法动态生成ZIP文件并将其发送到用户的浏览器进行下载
我有音频剪辑的URL。我想从他们的URL下载所有这些剪辑,并想把所有这些剪辑放在一个zip文件中。如果我在代码中指定硬编码路径,我就能够在指定的位置以zip格式成功下载所有剪辑。但是,我想生成一个弹出窗口,提示用户指定下载该zip文件的路径。 这是我的密码:Java 无法动态生成ZIP文件并将其发送到用户的浏览器进行下载,java,grails,response,zipfile,savefiledialog,Java,Grails,Response,Zipfile,Savefiledialog,我有音频剪辑的URL。我想从他们的URL下载所有这些剪辑,并想把所有这些剪辑放在一个zip文件中。如果我在代码中指定硬编码路径,我就能够在指定的位置以zip格式成功下载所有剪辑。但是,我想生成一个弹出窗口,提示用户指定下载该zip文件的路径。 这是我的密码: ByteArrayOutputStream baos = new ByteArrayOutputStream() ZipOutputStream zipFile = new ZipOutputStream(baos) for
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;
所以,在这里,它在循环中对每个剪辑进行迭代,下载它并使其成为zip文件的一部分
如果我做错了什么,请引导我,或者我可以通过其他方式实现这一点。
感谢您的时间、考虑和指导。您的行动应该切实有效。 浏览器另存为。。对话框自动打开,如果它看到带有附件的内容处置标题;filename=aaa.zip作为响应中的值 尝试删除引号:
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();
}
您是否使用相同的文件名保存所有文件?否,该zip文件中的每个文件都有不同的名称。您的代码声明的是相反的。使用相同的文件名变量命名所有zipEntries如果您仔细看,我在循环的每次迭代中都在更改“fileName”变量如果getSessionId做了我认为它做的事情,那么它应该不仅在整个循环中,而且在整个会话中都具有相同的值:您在浏览器/日志中看到了什么?如果将BAO转储到磁盘,它是有效的zip文件吗?它根本不会生成zip文件