项目中有个需求,需要在系统动态展示图片,图片是通过流的形式从数据库读取的,网上查的资料要么说通过ext 组件的html属性,html:<img src='../../xx.jpg'/>,要么说通过流在项目文件夹生成图片,然后再用上面的方法来显示,都不能满足要求。于是我就试着将上面的src换成一个到action的请求,竟然很好的实现了需求。
ext js页面,在一个tabpanel里放置一个元素,该元素的html属性如上面所说设置
mainTabPanel.add({
title : '测试',
id : 'test',
autoScroll:true,
html : '<img src="/.../commonProcessController/getPicture.do?param=id" style="position:absolute;left:0px;top:0px;">'
});
在controller里面,配置如下映射:
@RequestMapping(value = "/getPicture.do", method = RequestMethod.GET)
public void getProcessPicture(HttpServletRequest request,HttpServletResponse response,String id) throws Exception {
InputStream inputStream = processEngine.getRepositoryService().getResourceAsStream(processDefinition.getDeploymentId (), processDefinition.getKey()+".png");
byte[] buffer = new byte[inputStream.available()];
inputStream.read(buffer);
inputStream.close();
response.reset();
OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
toClient.write(buffer);
toClient.flush();
toClient.close();
}
大功告成,效果就不截图了。