|
背景:有两个平台,A和B,A平台负责图片的上传,B平台负责图片的展示
今天第一次遇到读二进制图片并显示页面的问题,在网上淘了淘,加上自己实际的情况得以实现,现记录在此,以作备忘。
1.项目中业务处理放在ejb中,ejb直接跟数据库打交道。下面为ejb层方法。
//讲图片以二进制形式返回给Action控制层
public byte[] getMenuInfo(String type)
{
//final byte[] result =new byte[1024];
final List<byte[]> result=new ArrayList<byte[]>();
String sql = "select F_VALUE from suitpicture where f_type=?";
getJdbcTemplate().query(sql, new Object[] { type},
new RowMapper() {
public Object mapRow(ResultSet rs, int index)throws SQLException
{
Blob img=rs.getBlob("F_VALUE");
long size=img.length();
byte[] bs=img.getBytes(1, (int)size);
result.add(bs);
return null;
}
});
if(result!=null&&result.size()>0)
return result.get(0);
else return null;
}
2.Action 层方法
//获取套餐图片信息
public String getMenuInfo()
{
String type=getRequest().getParameter("type");
try {
BusinessRemote br = EJBHomeFactory.getInstance().lookupBusinessRemote();
byte[] bs=br.getMenuInfo(type);
ServletOutputStream outs=getResponse().getOutputStream();
outs.write(bs);
outs.flush();
return null;
} catch (Exception e)
{
addErr("获取图片信息发生错误!");
log.error("获取图片信息发生错误!",e);
}
return null;
}
3.jsp(展示层)
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<img src="getMenuInfo.html?type=XYTC"/>
</body>
</html> |
|
|