【发布时间】:2016-11-20 00:53:56
【问题描述】:
在this answer,一个人建议使用IOUtils.copy,但他没有关闭OutputStream。这是他的例子:
InputStream is = new URL(imgUrl).openStream();
OutputStream os = servletResponse.getOutputStream();
IOUtils.copy(is, os);
is.close();
我检查了 IOUtils 中 copy 方法的 javadocs,没有任何信息表明 OutputStream 将自动关闭,所以我很好奇是否需要在该示例中关闭 OutputStream?
【问题讨论】:
-
如果您确实打算关闭您的资源或 Steam,请确保在
finally块内调用close()以确保它始终被执行。或者,在try-with-resources statement 中打开您的资源
标签: java apache-commons outputstream