Java 使用SpringMVC在jsp中显示来自相对路径的所有图像
我将图像上传到一个文件夹,并将相对路径保存到数据库。但当我尝试显示所有图像时,它只显示一个图像(第一个),然后我得到一个错误:Java 使用SpringMVC在jsp中显示来自相对路径的所有图像,java,image,spring-mvc,path,relative-path,Java,Image,Spring Mvc,Path,Relative Path,我将图像上传到一个文件夹,并将相对路径保存到数据库。但当我尝试显示所有图像时,它只显示一个图像(第一个),然后我得到一个错误: SEVERE: Servlet.service() for servlet jsp threw exception java.lang.IllegalStateException: getOutputStream() has already been called for this response at org.apache.catalina.conne
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";
}
@RequestMapping(value=“/allpictures”,method=RequestMethod.GET)
公共字符串getAllImage(ModelMap模型、HttpServletRequest请求、HttpServletResponse响应、SecurityContextHolderAwareRequestWrapper请求)
{
字符串名称=request.getUserPrincipal().getName();
model.addAttribute(“用户名”,名称);
集合图像=img.showAllPictures();
用于(上传图像上传图像:图像)
{
System.out.println(“name”+uploadImage.getFileName());
System.out.println(“path”+uploadImage.getFilePath());
System.out.println(“上传”+上传图像);
输出流输出;
尝试
{
out=response.getOutputStream();
InputStream InputStream=新文件InputStream(新文件(uploadImage.getFilePath());
字节[]buf=新字节[32*102400];
int nRead=0;
而((nRead=inputStream.read(buf))!=-1)
{
输出。写入(buf,0,nRead);
}
model.addAttribute(“全部”,上传图像);
out.flush();
out.close();
返回“所有图片”;
}
捕获(IOE异常)
{
e、 printStackTrace();
}
}
返回“重定向:/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>
/${all}“width=“70”height=“70”>
解决了吗?同样的问题