繁体   English   中英

无法即时生成ZIP文件并将其发送到用户的浏览器进行下载

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM