【发布时间】:2022-01-20 07:30:58
【问题描述】:
这个问题我觉得很难解释,但我尽力了。
目前,我有一个方法可以返回一个带有 Zip 文件的 InputStream,我必须将其添加到主 zip 文件中。问题是当我在OutputStream 中写入内容时,它会覆盖以前写入的数据。我尝试使用ZipOutputStream 和ZipEntries 但这会重新压缩文件并做一些奇怪的事情,所以它不是一个解决方案。我必须使用且不可协商的东西是:
-
使用返回 InputStream 的方法检索文件
-
使用
IOUtils.copy()方法下载文件(如果您有其他解决方案允许我通过浏览器下载文件,这可能是可选的)
这是目前为止的代码:
OutputStream os = null;
InputStream is = null;
try {
os = response.getOutputStream();
for (int i = 0; i < splited.length; i += 6) {
String[] file= //an array with the data to retrieve the file
is = FileManager.downloadFile(args);
int read;
byte[] buffer = new byte[1024];
while (0 < (read = is.read(buffer))) {
os.write(buffer, 0, read);
}
}
} catch (Exception ex) {
//Exception captures
}
response.setHeader("Content-Disposition", "attachment; filename=FileName");
response.setContentType("application/zip");
IOUtils.copy(is, os);
os.close();
is.close();
return forward;
【问题讨论】:
-
您的循环已经将整个数据从
is复制到os,那么在复制完所有内容后调用IOUtils.copy(is, os);有什么意义呢?而且你明白你的代码只会关闭最后一个InputStream吗?
标签: java file download java-stream zip