读取图片文件内容
public ByteArrayOutputStream OutputBlob(String sql,String fieldName) throws SQLException{ ByteArrayOutputStream bao = new ByteArrayOutputStream();
conn = ? //你的连接取得,这个你自己写,因为我的跟你不一样
st = conn.createStatement();
ResultSet rs = st.executeQuery(sql);
if (rs.next()) {
//到数据库的输出流
oracle.sql.BLOB blob = (oracle.sql.BLOB)rs.getBlob(fieldName);
InputStream blobReader = blob.getBinaryStream();
byte[] b = new byte[blob.getBufferSize()];
int len=-1;//System.out.println("start to read");
while ((len=blobReader.read(b)) != -1) {
bao.write(b, 0, len);
}
bao.flush();
bao.close();
}else{
return null;
} st.close(); //关闭资源
return bao;
}
catch (Exception ex) {
//关闭资源
return null;
}
}
然后写留到文件,再页面显示,最后删除文件(怕你生成太多文件)
public ByteArrayOutputStream OutputBlob(String sql,String fieldName) throws SQLException{ ByteArrayOutputStream bao = new ByteArrayOutputStream();
conn = ? //你的连接取得,这个你自己写,因为我的跟你不一样
st = conn.createStatement();
ResultSet rs = st.executeQuery(sql);
if (rs.next()) {
//到数据库的输出流
oracle.sql.BLOB blob = (oracle.sql.BLOB)rs.getBlob(fieldName);
InputStream blobReader = blob.getBinaryStream();
byte[] b = new byte[blob.getBufferSize()];
int len=-1;//System.out.println("start to read");
while ((len=blobReader.read(b)) != -1) {
bao.write(b, 0, len);
}
bao.flush();
bao.close();
}else{
return null;
} st.close(); //关闭资源
return bao;
}
catch (Exception ex) {
//关闭资源
return null;
}
}
然后写留到文件,再页面显示,最后删除文件(怕你生成太多文件)
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货