Java 不正确的资源关闭或释放
说明: 在下面的代码中,我做错了什么在下面的代码中,在我的代码审计中,我得到了不正确的资源关闭或释放。 我试着从下面的代码中删除关闭和刷新Java 不正确的资源关闭或释放,java,xml,security,Java,Xml,Security,说明: 在下面的代码中,我做错了什么在下面的代码中,在我的代码审计中,我得到了不正确的资源关闭或释放。 我试着从下面的代码中删除关闭和刷新 File someFile = new File(fileName); fos = new FileOutputStream(someFile); fos.write(data); fos.flush();
File someFile = new File(fileName);
fos = new FileOutputStream(someFile);
fos.write(data);
fos.flush();
fos.close();
主要代码:
FileOutputStream fos=null;
try {
Hashtable hash = responseBlob.getAllAttachments();
Enumeration e = hash.elements();
while (e.hasMoreElements()) {
SBADataAttach tmpAttach = (SBADataAttach) e.nextElement();
String tag = tmpAttach.getTag();
byte[] data = tmpAttach.getData();
// encode compressed file
if (hasTag(tag))
mimeResponse.addPart(tag, Base64.encodeBytes(data)
.getBytes());
else
mimeResponse.addPart(tag, data);
try {
if (this.configData.getBlobPath() != null)
{
// save compressed file
String fileName = File.separatorChar + "tmp"
+ File.separatorChar + tag;
Log.theLogger.debug("XisServlet.process() ... "
+ "Save compressed file = " + fileName);
File someFile = new File(fileName);
fos = new FileOutputStream(
someFile);
fos.write(data);
fos.flush();
fos.close();
}
} catch (Exception zipe) {
Log.theLogger.error(zipe.getMessage(), zipe);
}
}
} catch (SBADataException sde) {
// cannot detach files from blob
Log.theLogger.error(sde.getMessage(), sde);
}
finally {
try {
if( fos!=null ) {
fos.close();
}
} catch(IOException e) {
Log.theLogger.error(e.getMessage(), e);
}
}
我没有得到任何错误。但是,在Appsec finding i Get Resource Shutdown or Release中,如果在新文件OutputStreamSomeFile之间有一些代码,您的代码可能无法关闭fos;fos.close抛出一个异常。我真的需要fos.close吗;当我在最后执行fos.close时,也在最后{try{if fos!=null{fos.close;}}catchIOException e{Log.theLogger.errore.getMessage,e;}}使用try with resource语句。我已经更改了要尝试的代码{//open resources File someFile=new filefilefilefilename;fos=new FileOutputStreamsomeFile;fos.writedata;}catchException e1{//handle exception}最后{//close resources fos.flush;fos.close;}
try{
//open resources
File someFile = new File(fileName);
fos = new FileOutputStream(someFile);
fos.write(data);
}
catch(Exception e1){
//handle exception
}finally{
//close resources
fos.flush();
fos.close();
}
}