您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

如何为“虚拟文件”列表创建ZIP文件并将其输出到httpservletresponse

如何为“虚拟文件”列表创建ZIP文件并将其输出到httpservletresponse

原来我是个白痴:)正在“创建”的文件保存到无效路径并吞没了异常,因此我认为它是在“创建”的。但是,当我尝试实例化一个新的FileInputStream时,它抱怨该文件不存在(正确地如此)。我头脑清醒,并假设java.io.File对象实际上在某处包含文件信息。但是正如埃里克森指出的那样,这是错误的。

感谢Ralph提供的代码,在解决了无效的路径问题后才使用它。

我的代码

ZipOutputStream out = new ZipOutputStream(response.getOutputStream());
byte[] buf = new byte[1024];

File file;
InputStream in;
// Loop through entities
for (TitleProductAccountApproval tpAccountApproval : tpAccountApprovals) {
    // Generate the file    
    file = xmlManager.getXML(
        tpAccountApproval.getTitleProduct().getTitleProductId(), 
        tpAccountApproval.getAccount().getAccountId(), 
        username);

    // Write to zip file
    in = new FileInputStream(file);
    out.putNextEntry(new ZipEntry(file.getName()));

    int len;
    while ((len = in.read(buf)) > 0) {
        out.write(buf, 0, len);
    }

    out.closeEntry();
    in.close();
}

out.close();
Jave 2022/1/1 18:19:59 有413人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶