整体思路:从数据库中读取图片二进制流并保存在InputStream中,通过ByteArrayOutputStream将InputStream转换到byte数组中并传递到页面。
/**
* 显示png图片 (文件下载)
* @param pdId
* @return
*/
@RequestMapping("/processDefinitionAction_showImg")
public void showImg(HttpServletResponse httpServletResponse, String pdId) throws IOException {
//从数据库中获取流程图的二进制数据
InputStream inputStream=processDefinitionService.findImgStream(pdId);
ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
byte[] buff = new byte[100]; //buff用于存放循环读取的临时数据
int rc = 0;
//将输入流转换为字符数组输出流
while ((rc = inputStream.read(buff, 0, 100)) > 0) {
swapStream.write(buff, 0, rc);
}
byte[] img = swapStream.toByteArray(); //in_b为转换之后的结果
//设置响应头
//httpServletResponse.setHeader("content-type", "image/png;charset=UTF-8");
httpServletResponse.setContentType("image/png");
OutputStream os = httpServletResponse.getOutputStream();
os.write(img);
os.flush();
os.close();
}
网友评论