title: java.io.IOException: Stream closed
body: <p>For multiple image retrieval I am calling a <code>PhotoHelperServlet</code> with an anchor tag to get imageNames(multiple images) as follows</p>
  
  <p><code>PhotoHelperServlet</code> to get names of <code>Images</code></p>
  
  <pre><code>protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  
  // Getting userid from session
  
  Image image = new Image();
  image.setUserid(userid);
  
  ImageDAO imageDAO = new ImageDAO();
  
  try {
  
      List&lt;Image&gt; imageId = imageDAO.listNames(image);
  
      if (imageId == null) { 
          // check if imageId is retreived
      }
  
      request.setAttribute("imageId", imageId);
  
      //Redirect it to home page
      RequestDispatcher rd = request.getRequestDispatcher("/webplugin/jsp/profile/photos.jsp");
      rd.forward(request, response);
  
  catch (Exception e) {
      e.printStackTrace();
  }
  </code></pre>
  
  <p>In ImageDAO listNames() method :</p>
  
  <pre><code>public List&lt;Image&gt; listNames(Image image) throws IllegalArgumentException, SQLException, ClassNotFoundException {
  
      Connection connection = null;
      PreparedStatement preparedStatement = null;
      ResultSet resultset = null;
      Database database = new Database();
      List&lt;Image&gt; imageId = new ArrayList&lt;Image&gt;();
  
      try {
  
          connection = database.openConnection();
          preparedStatement = connection.prepareStatement(SQL_GET_PHOTOID);                  
          preparedStatement.setLong(1, image.getUserid());
          resultset = preparedStatement.executeQuery();
  
          while(resultset.next()) {
              image.setPhotoid(resultset.getLong(1));
              imageId.add(image);
          }
  
      } catch (SQLException e) {
          throw new SQLException(e);
      } finally {
          close(connection, preparedStatement, resultset);
      }
      return imageId;
  }
  </code></pre>
  
  <p>In JSP code:</p>
  
  <pre><code>&lt;c:forEach items="${imageId}" var="imageid"&gt;
      &lt;img src="Photos/${imageid}"&gt;
  &lt;/c:forEach&gt;
  </code></pre>
  
  <p>In PhotoServlet doGet() method to get a photo:</p>
  
  <pre><code>protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  
  String imageid = request.getPathInfo().substring(1);
  
  if(imageid == null) {
      // check for null and response.senderror
  }
  
  ImageDAO imageDAO = new ImageDAO();
  
  try {
  
      Image image = imageDAO.getPhotos(imageid);
  
      if(image == null) {}
  
      BufferedInputStream input = null;
      BufferedOutputStream output = null;
  
      try {
  
          input = new BufferedInputStream(image.getPhoto(), DEFAULT_BUFFER_SIZE);
          output = new BufferedOutputStream(response.getOutputStream(), DEFAULT_BUFFER_SIZE);
  
          // Write file contents to response.
          byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
          int length;
          while ((length = input.read(buffer)) &gt; 0) {
              output.write(buffer, 0, length);
          }
      } finally {
          if (output != null) try { output.close(); } catch (IOException logOrIgnore) {}
          if (input != null) try { input.close(); } catch (IOException logOrIgnore) {}
      }
  
  } catch(Exception e) {
      e.printStackTrace();
  }
  </code></pre>
  
  <p>In ImageDAO getPhotos() method</p>
  
  <pre><code>public Image getPhotos(String imageid) throws IllegalArgumentException, SQLException, ClassNotFoundException {
  
      Connection connection = null;
      PreparedStatement preparedStatement = null;
      ResultSet resultset = null;
      Database database = new Database();
      Image image = new Image();
  
      try {
  
          connection = database.openConnection();
          preparedStatement = connection.prepareStatement(SQL_GET_PHOTO);                  
          preparedStatement.setString(1, imageid);
          resultset = preparedStatement.executeQuery();
  
          while(resultset.next()) {
              image.setPhoto(resultset.getBinaryStream(1));
          }
  
      } catch (SQLException e) {
          throw new SQLException(e);
      } finally {
          close(connection, preparedStatement, resultset);
      }
      return image;
  }
  </code></pre>
  
  <p>In web.xml</p>
  
  <pre><code>&lt;!-- Getting each photo --&gt;
  &lt;servlet&gt;
      &lt;servlet-name&gt;Photos Module&lt;/servlet-name&gt;
      &lt;servlet-class&gt;app.controllers.PhotoServlet&lt;/servlet-class&gt;
  &lt;/servlet&gt;
  &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;Photos Module&lt;/servlet-name&gt;
      &lt;url-pattern&gt;/Photos/*&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
  
  &lt;!-- Getting photo names --&gt;
  &lt;servlet&gt;
      &lt;servlet-name&gt;Photo Module&lt;/servlet-name&gt;
      &lt;servlet-class&gt;app.controllers.PhotoHelperServlet&lt;/servlet-class&gt;
  &lt;/servlet&gt;
  &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;Photo Module&lt;/servlet-name&gt;
      &lt;url-pattern&gt;/Photo&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
  </code></pre>
  
  <p>Question:</p>
  
  <p>I am getting following Exception:</p>
  
  <pre><code>java.io.IOException: Stream closed
  </code></pre>
  
  <p>on this Line:</p>
  
  <pre><code>at app.controllers.PhotoServlet.doGet(PhotoServlet.java:94)
  while ((length = input.read(buffer)) &gt; 0) {
  </code></pre>
  
  <p>The full Exception:</p>
  
  <pre><code>java.io.IOException: Stream closed
  at java.io.BufferedInputStream.getInIfOpen(BufferedInputStream.java:134)
  at java.io.BufferedInputStream.read1(BufferedInputStream.java:256)
  at java.io.BufferedInputStream.read(BufferedInputStream.java:317)
  at java.io.FilterInputStream.read(FilterInputStream.java:90)
  at app.controllers.PhotoServlet.doGet(PhotoServlet.java:94)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
  at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
  at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
  at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
  at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
  at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:498)
  at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
  at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
  at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
  at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
  at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:394)
  at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:243)
  at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
  at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:166)
  at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
  at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
  at java.lang.Thread.run(Thread.java:662)
  </code></pre>