struts2读取二进制图片并显示
struts.xml配置
<action name="activitiAction_*" class="activitiAction" method="{1}"></action>
action文件xxxAction.java:
/**
* 生成图片
*
* @return
* @throws IOException
*/
public void generateDiagram() throws IOException
{
activitiService.generateDiagram("277501");//调用service实现类中的方法
}
service实现类ActivitiService.java:
/**
* 生成图片
*/
@Override
public void generateDiagram(String processInstanceId)
{
//拿到response
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("p_w_picpath/png");
//拿到servlet的输出流
ServletOutputStream out=null;
try {
out = response.getOutputStream();
} catch (IOException e) {
}
//以下方法是为了拿到图片输入流
ProcessInstance pi =runtimeService.createProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();
BpmnModel bm=repositoryService.getBpmnModel(pi.getProcessDefinitionId());
List<String> activeIds=runtimeService.getActiveActivityIds(pi.getId());
DefaultProcessDiagramGenerator dpg = new DefaultProcessDiagramGenerator();
InputStream is = dpg.generateDiagram(bm, "png", activeIds,
Collections.<String> emptyList(), processEngine
.getProcessEngineConfiguration().getActivityFontName(),
processEngine.getProcessEngineConfiguration().getLabelFontName(),
null, 1.0);
//读取输入流
byte[] bt = new byte[1024];
int c;
try {
while ((c = is.read(bt)) != -1) {
for (int i = 0; i < c; i++)
out.write(bt[i]);
}
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
浏览器访问方式:
http://localhost:8080/oa_learn/activitiAction_generateDiagram.action
转载于:https://blog.51cto.com/pjwqh/1655343