3

我将图像上传到文件夹并将相对路径保存到数据库。但是当我尝试显示所有图像时,它只显示一个图像(第一个),然后我得到一个错误:

   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)

任何人怎么知道我可以解决这个问题?各种帮助将不胜感激。

这是用于读取文件的控制器:

    @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();

    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";
}

这是jsp代码:

<%@ 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>
4

0 回答 0