display all images from relative path

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • iraj
    New Member
    • Dec 2012
    • 1

    display all images from relative path

    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:
    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)
    Please how can I show all images in one jsp page?

    Here is the controller for reading the files:
    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<UploadImage> images = img.showAllPictures();
    		Iterator<UploadImage> it = images.iterator();
    		OutputStream out;
    		InputStream inputStream;
    
    		while (it.hasNext())
    		{
    			UploadImage iterator = it.next();
    			try
    			{
    				out = response.getOutputStream();
    				inputStream = new FileInputStream(new File(iterator.getFilePath()));
    				byte[] buf = new byte[32 * 10000000];
    				for (int i = 0; i < buf.length; i++)
    				{
    					int nRead = 0;
    					while ((nRead = inputStream.read(buf)) != -1)
    					{
    						out.write(buf, 0, nRead);
    						
    						model.addAttribute("all", iterator);
    						inputStream.close();
    						out.flush();
    						out.close();
    					}
    				}
    
    			}
    			catch (Exception e)
    			{
    				e.printStackTrace();
    			}
    		}
    
    		return "allpictures";
    	}
    And this is the jsp code:

    Code:
        <%@ page language="java" contentType="text/html; charset=US-ASCII"
        pageEncoding="US-ASCII"%>
        <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
         <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
         <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
         <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4   /loose.dtd">
         <html>
         <head>
          <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
          </head>
         <body>
         <c:forEach var="all" items="${all}">
         <img alt="pictures3" src="<%=request.getContextPath()%>/${all}" width="70"     height="70">		
         </c:forEach>
    			
         </body>
         </html>
    Thanks for all help I can get.
  • Anas Mosaad
    New Member
    • Jan 2013
    • 185

    #2
    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.

    Comment

    Working...