关闭输入流?

时间:2018-04-09 14:52:24

标签: java servlets

应用程序具有下面的旧servlet代码,其中没有任何输入流被关闭。我不确定这是正确还是错误。我应该关闭每个流(ByteArrayInputStream,InputStream,PrintWriter)吗?

    public void doPost(HttpServletRequest reqest, HttpServletResponse response)  
                throws ServletException, IOException { 

            PrintWriter out=response.getWriter();
    try{
    InputStream xml = new BufferedInputStream(request.getInputStream());
    byte[] xmlbytes =getBytes(xml);
    if(xmlBytes.lenght>0){

    ByteArrayInputStream byteIn = new ByteArrayInputStream(xmlbytes);
    ObjectInputStream in = new ObjectInputStream(byteIn);
    // some logic here//
    }
}catch(Exception e)
    { out.println("Exception");

    }

    }


    Private static byte[] getBytes(InputStream is){
    ByteArrayOutputStream os = new ByteArrayOutputStream() ;
    BufferedInputStream is= new BufferdInputStream(is);
    try {
    Boolean b= false;
    while(!(b)){
    int i = is.read();
    if(i == -1)
    b= true;
    else o.write(i);
    }
    }catch(IOException e){
    return null;
    }
    return os.toByteArray();
    }

0 个答案:

没有答案