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:
Please how can I show all images in one jsp page?
Here is the controller for reading the files:
And this is the jsp code:
Thanks for all help I can get.
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)
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";
}
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>
Comment