我已阅读以下主题: Should one call .close() on HttpServletResponse.getOutputStream()/.getWriter()?
但是如果我使用下面的结构呢:
ZipOutputStream zos = new ZipOutputStream(response.getOutputStream());
我应该关闭它还是容器会代替我来做?
最佳答案
是的,您应该显式调用ZipOutputStream
上的close()
方法,here's close()
方法的代码。它执行以下操作:
- 调用父类(super class)的
close()
,在我们的例子中是DeflaterOutputStream
close()
在调用底层上的
。close()
之前调用finish()
输出流
DeflaterOutputStream
的 此 finish()
方法写入剩余的压缩数据,因此如果您不在 ZipOutputStream< 上调用
明确。所以,我建议调用它。close()
,您可能会得到一些未写入的数据
关于java - 我应该通过 response.getOutputStream() 显式关闭 ZipOutputStream 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46400625/