找工作複習中--Struts2輸出圖片流到頁面

前言:今天做個功能需要展示圖片到頁面,並不是下載,在網上搜了老半天,大部分都是下載,有的話也是只能在IE下進行輸出,其它瀏覽器就都是下載了。

Action代碼:

	public String processImage(){
		String key = ServletActionContext.getRequest().getParameter("key");
		if(StringUtils.isEmpty(key)){
			return "nofindImage";//沒有找到圖片
		}
		ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
                  .processDefinitionKey(key)
                  .singleResult();
		String diagramResourceName = processDefinition.getDiagramResourceName();
		InputStream imageStream = repositoryService.getResourceAsStream(processDefinition.getDeploymentId(), diagramResourceName);
		ServletActionContext.getContext().put("imageStream", imageStream);
		return "processImage";
	}

注:這裏將InputStream放到了Struts2的對象站裏面

此外,也可以通過在action中添加一個getInputName()的方法,在這個方法裏面返回上面的InputStream

XML配置:在action中添加結果

	<result name="processImage" type="stream">
                <param name="contentType">image/png</param>
                <param name="inputName">imageStream</param>
                <param name="bufferSize">4096</param>
         </result>


注:inputName的值對應了我們在action中放入對象棧的key,因爲StreamResult的源碼中獲得inputName就是從棧中獲取。

其它說明:網上部分代碼是在action中將InputStream通過Response直接輸出,並返回一個null,這樣在ie下是可以直接顯示圖片,在其他瀏覽器裏面都是下載的方式。

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章