title: Display all images from relative path in jsp with spring mvc
body: <p>I upload images to a folder and save relative path to database. But when i try to display all images it shows only one image (the first one) and then I get an error:</p>
  
  <pre><code>   SEVERE: Servlet.service() for servlet jsp threw exception
      java.lang.IllegalStateException: getOutputStream() has already been called for this response
  at org.apache.catalina.connector.Response.getWriter(Response.java:636)
  at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:214)
  </code></pre>
  
  <p>Any body how knows how I can fix the problem?
  All kind of helps would be appreciated.</p>
  
  <p>Here is the controller for reading the files:</p>
  
  <pre><code>    @RequestMapping(value = "/allpictures", method = RequestMethod.GET)
  public String getAllImages(ModelMap model, HttpServletRequest req, HttpServletResponse response, SecurityContextHolderAwareRequestWrapper request)
  {
  
      String name = request.getUserPrincipal().getName();
      model.addAttribute("username", name);
  
      Collection&lt;UploadImage&gt; images = img.showAllPictures();
  
      for (UploadImage uploadImage : images)
      {
          System.out.println("name "+uploadImage.getFileName());
          System.out.println("path "+uploadImage.getFilePath());
          System.out.println("upload "+uploadImage);
          OutputStream out;
          try
          {
              out = response.getOutputStream();
              InputStream inputStream = new FileInputStream(new File(uploadImage.getFilePath()));
  
              byte[] buf = new byte[32 * 102400];
              int nRead = 0;
              while ((nRead = inputStream.read(buf)) != -1)
              {
                  out.write(buf, 0, nRead);
              }
              model.addAttribute("all", uploadImage);
              out.flush();
              out.close();
              return "allpictures";
          }
          catch (IOException e)
          {
              e.printStackTrace();
          }
      }
      return "redirect:/index";
  }
  </code></pre>
  
  <p>And this is the jsp code:</p>
  
  <pre><code>&lt;%@ page language="java" contentType="text/html; charset=US-ASCII"
  pageEncoding="US-ASCII"%&gt;
  &lt;%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%&gt;
   &lt;%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%&gt;
   &lt;%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%&gt;
   &lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4   /loose.dtd"&gt;
   &lt;html&gt;
   &lt;head&gt;
    &lt;meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"&gt;
    &lt;/head&gt;
   &lt;body&gt;
   &lt;c:forEach var="all" items="${all}"&gt;
   &lt;img alt="pictures3" src="&lt;%=request.getContextPath()%&gt;/${all}" width="70"     height="70"&gt;        
   &lt;/c:forEach&gt;
  
   &lt;/body&gt;
   &lt;/html&gt;
  </code></pre>