JAVA+EXTJS+ORACLE在页面展示BLOB类型保存的图片
JAVA后台
/**
* 前台展示图片
* @throws Exception
*/
@SkipPrivilege
public void showImage() throws Exception{
String jsonString = null;
try {
if(null!=cigCode&&!"".equals(cigCode)&&null!=inoutStatus&&!"".equals(inoutStatus)){
jsonString="\"data\":{'name':'" + cigCode + "','url':'sellCigInoutSummaryQuery!outImageStream.action?cigCode="+cigCode+"&inoutStatus="+inoutStatus+"'}";
}else{
jsonString="\"data\":{'name':'" + cigCode + "','url':'images/nofoundimage.png'}";
}
} catch (Exception e) {
throw e;
}
jsonString = "{\"success\":true," + jsonString + "}";
response(jsonString);
}
/**
* 处理BLOB类型的图像流,输出到前台
* @throws Exception
*/
@SkipPrivilege
public void outImageStream() throws Exception{
Connection conn = null;
ResultSet rs = null;
PreparedStatement pstmt = null;
Session session = null;
ServletOutputStream out =null;
BufferedInputStream imgStream =null;
try {
session = dao.getSessionFactory().openSession();
conn = session.connection();
String sql = " select t.cig_photo from t_iso_rg_sell_cig_inout t where 1=1 "
+ " and t.cig_code = ? and t.inout_status = ? ";
pstmt= conn.prepareStatement(sql);
pstmt.setString(1,cigCode);
pstmt.setString(2,inoutStatus);
rs=pstmt.executeQuery();
if (rs.next()) {
Blob photoBlob = rs.getBlob("cig_photo");
out = response.getOutputStream();
int nSize =0;
byte[] bye;
int size = -1;
if(null!=photoBlob){
imgStream = new BufferedInputStream(photoBlob.getBinaryStream());//imgStream = ;
nSize = (int)photoBlob.length();
bye = new byte[nSize];
size = imgStream.read(bye,0,bye.length);
if(size>0){
out.write(bye,0,size);
}
//显示默认图片
} else {
//得到默认图片的路径
String pathStr = request.getSession().getServletContext().getRealPath("images/nofoundimage.png");
File file = new File(pathStr);
imgStream = new BufferedInputStream(new FileInputStream(file));
nSize = (int)imgStream.available();
bye = new byte[nSize];
size = imgStream.read(bye,0,bye.length);
out.write(bye,0,size);
}
imgStream.close();
out.flush();
out.close();
}
} catch (Exception e) {
throw e;
}finally{
closeAll(conn, rs, pstmt);
}
}
/**
* 关闭连接
* @param Connection
* @param ResultSet
* @param PreparedStatement
* @throws Exception
*/
public void closeAll(Connection conn, ResultSet rs, PreparedStatement pstmt) throws Exception {
try {
if (null != rs) {
rs.close();
}
if (null != pstmt) {
pstmt.close();
}
if (null != conn) {
conn.close();
}
} catch (Exception e) {
throw e;
}
}
EXTJS前台展示
var imageRecord = new Ext.data.Record.create([
{name:'name'},
{name:'url'}
]);
var imageReader = new Ext.data.JsonReader({root:'data',idProperty:'name'},imageRecord);
var imageProxy = new Ext.data.HttpProxy({url:'sellCigInoutSummaryQuery!showImage.action'});
var imageStore = new Ext.data.Store({
proxy: imageProxy,
reader: imageReader,
listeners: {exception:storeExceptionHandle}
});
//页面展示图片
var tpl = new Ext.XTemplate(
'<tpl for=".">',
'<div class="thumb-wrap">',
'<div class="thumb"><img src="{url}" width="125" height="140"></div>',
'</tpl>',
'<div class="x-clear"></div>'
);
imageView = new Ext.DataView({
store: imageStore,
loadingText: '正在读取图片...',
tpl: tpl,
height: 140,
autoScroll: true,
singleSelect: true,
overClass:'x-view-over',
itemSelector:'div.thumb-wrap',
emptyText: '没有图片'
});
this.imagePanel = new Ext.Panel({
title : "图片展示",
region:"center",
frame: true,
border: true,
width : 125,
height : 140,
items:[imageView]
});