struts2图片显示

时间:2023-03-09 08:21:45
struts2图片显示

struts2图片显示即是文件下载

一、配置struts.xml

struts.xml中配置stream结果类型,并配置contentType、inputName、contentDisposition、bufferSize参数即可

<action name="readImgAction" class="com.bk.eserver.web.action.ImgAction" method="readImg" >
<result type="stream">
<param name="contentType">application/octet-stream</param>
<param name="inputName">inputStream</param>
<param name="contentDisposition">attachment;filename=${fileName}</param>
<param name="bufferSize">4096</param>
</result>
</action>

二、ImgAction

public class ImgAction extends WebSupport {
private InputStream inputStream; /**
* 读取图片
*
* @return
*/
public String readImg() {
try {
inputStream = new FileInputStream(new File("D:\\meinv.jpg"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return SUCCESS;
} public InputStream getInputStream() {
return inputStream;
} public void setInputStream(InputStream inputStream) {
this.inputStream = inputStream;
} }

三、要显示图片的JSP

<img  src="admin/readImgAction.do">

恩,页面上将显示如下struts2图片显示

struts2图片显示