原来我是个白痴:)正在“创建”的文件保存到无效路径并吞没了异常,因此我认为它是在“创建”的。但是,当我尝试实例化一个新的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();