从数据库中以输入流的方式读取出图片数据,然后在jsp页面中读取输入流,以图片的格式显示出图片。
InputStream中实际上就是流程图,然后就是以怎样的相应格式显示InputStream中的内容。
service层
/**
* 读取带跟踪的图片}
struts2 action中
//从数据库中得到运行流程图,带标红
public String readResource(){
HttpServletResponse response = ServletActionContext.getResponse();
HttpServletRequest request = ServletActionContext.getRequest();
ActivitiController activitiController = new ActivitiController();
InputStream imageStream = activitiController.readResource(processInstanceId);
request.setAttribute("inputStream", imageStream);
/*byte[] b = new byte[1024];
int len = -1;
try {
while ((len = imageStream.read(b, 0, 1024)) != -1) {
response.getOutputStream().write(b, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
}*/
return "showImage";
}
jsp页面 需要设置response的相应格式
<%
response.setContentType("image/png;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
InputStream is = (InputStream)request.getAttribute("inputStream");
byte[] b = new byte[1024];
int len = -1;
while((len = is.read(b, 0, 1024)) != -1) {
response.getOutputStream().write(b, 0, len);
// 防止异常:getOutputStream() has already been called for this response
out.clear();
out = pageContext.pushBody();
}
%>
或者
struts2 action中
//从数据库中得到运行流程图,带标红
public String readResource(){
HttpServletResponse response = ServletActionContext.getResponse();
HttpServletRequest request = ServletActionContext.getRequest();
ActivitiController activitiController = new ActivitiController();
InputStream imageStream = activitiController.readResource(processInstanceId);
request.setAttribute("inputStream", imageStream);
byte[] b = new byte[1024];
int len = -1;
try {
while ((len = imageStream.read(b, 0, 1024)) != -1) {
response.getOutputStream().write(b, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
在jsp页面中用
<img src="<%=basePath%>oa/leaveAction!readResource"/> 以图片的方式自动加载