By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,796 Members | 1,344 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,796 IT Pros & Developers. It's quick & easy.

display all images from relative path

P: 1
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:
Expand|Select|Wrap|Line Numbers
  1.        SEVERE: Servlet.service() for servlet jsp threw exception
  2.         java.lang.IllegalStateException: getOutputStream() has already been called for this response
  3.     at org.apache.catalina.connector.Response.getWriter(Response.java:636)
  4.     at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:214)
  5.  
  6.  
Please how can I show all images in one jsp page?

Here is the controller for reading the files:
Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3.        @RequestMapping(value = "/allpictures", method = RequestMethod.GET)
  4.     public String getAllImages(ModelMap model, HttpServletRequest req, HttpServletResponse response, SecurityContextHolderAwareRequestWrapper request)
  5.     {
  6.  
  7.         String name = request.getUserPrincipal().getName();
  8.         model.addAttribute("username", name);
  9.  
  10.         Collection<UploadImage> images = img.showAllPictures();
  11.         Iterator<UploadImage> it = images.iterator();
  12.         OutputStream out;
  13.         InputStream inputStream;
  14.  
  15.         while (it.hasNext())
  16.         {
  17.             UploadImage iterator = it.next();
  18.             try
  19.             {
  20.                 out = response.getOutputStream();
  21.                 inputStream = new FileInputStream(new File(iterator.getFilePath()));
  22.                 byte[] buf = new byte[32 * 10000000];
  23.                 for (int i = 0; i < buf.length; i++)
  24.                 {
  25.                     int nRead = 0;
  26.                     while ((nRead = inputStream.read(buf)) != -1)
  27.                     {
  28.                         out.write(buf, 0, nRead);
  29.  
  30.                         model.addAttribute("all", iterator);
  31.                         inputStream.close();
  32.                         out.flush();
  33.                         out.close();
  34.                     }
  35.                 }
  36.  
  37.             }
  38.             catch (Exception e)
  39.             {
  40.                 e.printStackTrace();
  41.             }
  42.         }
  43.  
  44.         return "allpictures";
  45.     }
  46.  
  47.  
And this is the jsp code:

Expand|Select|Wrap|Line Numbers
  1.     <%@ page language="java" contentType="text/html; charset=US-ASCII"
  2.     pageEncoding="US-ASCII"%>
  3.     <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
  4.      <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
  5.      <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
  6.      <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4   /loose.dtd">
  7.      <html>
  8.      <head>
  9.       <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
  10.       </head>
  11.      <body>
  12.      <c:forEach var="all" items="${all}">
  13.      <img alt="pictures3" src="<%=request.getContextPath()%>/${all}" width="70"     height="70">        
  14.      </c:forEach>
  15.  
  16.      </body>
  17.      </html>
  18.  
Thanks for all help I can get.
Dec 16 '12 #1
Share this Question
Share on Google+
1 Reply


100+
P: 185
Yes, that correct. You are allowed to get the response output stream only once.

Instead, you may set the value for attribute all to and array with the image names. If images are in public folder, you may add img tag for each one in the JSP.
If they are not in a public folder (e.g. under WEB-INF), you may create a servlet that takes the URI and return the image data as you do in your second snippet. Please take care that having an image that returns the content of a file in the server file system is a major security exposure. So, take care that the File are only under the target directory.
Jan 14 '13 #2

Post your reply

Sign in to post your reply or Sign up for a free account.