Struts2 action返回图片流

本文介绍如何在Struts2框架中,通过Action类处理并返回图片流到前端展示,包括struts.xml配置和Action类的实现细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

备用:


struts.xml代码:

<action name="getDocumentImg" class="JcOffDocumentAction" method="getDocumentImg"><!-- 获取文档图片流 -->
	<result type="stream">     
	    <param name="contentType">image/jpeg</param>     
	    <param name="inputName">inputStream</param>     
	</result>  
</action>


action类代码:

/**
 * 获取图片流
 * @return
 */
public String getDocumentImg() {
	model = (JcOffDocument)baseBO.load(model);
	String imgUrl = getAllPath(model);
	//String imgUrl = "/jczfoa/images/aa.jpg";
	
	if(!MyUtils.isFileExist(imgUrl, "")){//如果指定的图片不存在,显示默认图片
		imgUrl = getSession().getServletContext().getRealPath("/")+"images"+File.separator+"zw.jpg";
	}
	
	try {
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		InputStream input = new BufferedInputStream(new FileInputStream(imgUrl));
		byte[] bt = new byte[1024];
		while (input.read(bt) > 0) {
			bos.write(bt);
		}
		this.inputStream = new ByteArrayInputStream(bos.toByteArray());
		bos.close();
		input.close();
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return SUCCESS;
}
private ByteArrayInputStream inputStream;
	
public ByteArrayInputStream getInputStream() {
	return inputStream;
}
public void setInputStream(ByteArrayInputStream inputStream) {
	this.inputStream = inputStream;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值