Is is necessary to close the input stream returned from HttpServletRequest?

前端 未结 4 1875
自闭症患者
自闭症患者 2021-01-01 13:46

I\'ve got some production code that does something like:

HttpServletRequest httpServletRequest
...
DataInputStream dis = new DataInputStream(httpServletReque         


        
4条回答
  •  抹茶落季
    2021-01-01 14:14

    The thumb rule in I/O is, if you did not open/create the inputstream source yourself, then you do not necessarily need to close it as well. Here you are just wrapping the request's inputstream, so you don't necessarily need to close it.

    If you did open the input yourself by e.g. new FileInputStream("c:/file.ext") then you obviously need to close it yourself in the finally block. The container ought to do so under the hood.

提交回复
热议问题