资源关闭或释放不当

时间:2019-09-07 20:23:19

标签: java xml security

说明: 在下面的代码中,我在下面的代码中做错了什么,在我的代码审核中,我关闭或释放了不正确的资源。 我尝试从下面的代码中取出close和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中发现我得到了不正确的资源关闭或释放

1 个答案:

答案 0 :(得分:-1)

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();
                            }


                        }