下面这段代码的作用是压缩,会用到ZipOutputStream
ZipOutputStream zipOutputStream = null;
try {
zipOutputStream = new ZipOutputStream(new FileOutputStream(zipPath));
} catch (FileNotFoundException e) {
throw new IllegalArgumentException("zipPath error " + "", e);
} finally {
if (null != zipOutputStream) {
try {
zipOutputStream.close();
} catch (IOException e) {
logger.error("", e);
}
}
}
错误放大版1:
ZipOutputStream zipOutputStream = null;
try {
FileOutputStream fileOutputStream = new FileOutputStream(zipPath);
throw new RuntimeException();
zipOutputStream = new ZipOutputStream(fileOutputStream);
} catch (FileNotFoundException e) {
throw new IllegalArgumentException("zipPath error " + "", e);
} finally {
if (null != zipOutputStream) {
try {
zipOutputStream.close();
} catch (IOException e) {
logger.error("", e);
}
}
}
错误放大版2:
ZipOutputStream zipOutputStream = null;
try {
zipOutputStream = new ZipOutputStream(new FileOutputStream(zipPath),null);
} catch (FileNotFoundException e) {
throw new IllegalArgumentException("zipPath error " + "", e);
} finally {
if (null != zipOutputStream) {
try {
zipOutputStream.close();
} catch (IOException e) {
logger.error("", e);
}
}
}
这里我有一个疑问,在new FileOutputStream(zipPath)的过程中如果已经打开了这个文件,但是在new ZipOutputStream过程中出错了,这个时候zipOutputStream并没有创建成功,所以在finally中不会调用close方法,造成的结果就是这个文件不会被关闭。
所以这种关闭方式是不是会有漏洞,还是我的理由有问题?
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。
这个例子有漏洞,不过漏洞不在这里,而是 FileOutputStream。
如果 new ZipOutputStream()调用失败,这个构造方法会抛出运行时异常,而且不会关闭传递进来的 FileOutputStream 对象。
由于调用者也不持有该对象的引用,也无法释放资源,会造成打开的文件描述符没有关闭。
如果使用java8的话可以用 try with,或者在finally块里面释放FileOutputStream 对象持有的资源,这样是比较保险的做法。