画像をフォルダーにアップロードし、データベースへの相対パスを保存します。しかし、すべての画像を表示しようとすると、1 つの画像 (最初の画像) しか表示されず、エラーが発生します。
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>