从数据库中读取Blob对象图片并显示.doc
文本预览下载声明
从数据库中读取Blob对象图片并显示
第一种方法:
大致方法就是,从数据库中读出Blob的流来,写到页面中去:
Connection conn = DBManager.getConnection();
String sql = SELECT picture FROM teacher WHERE id=1;
PreparedStatement ps = null;
ResultSet rs = null;
InputStream is = null;
OutputStream os = null;
try {
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
if(rs.next()){
is = rs.getBinaryStream(1);
}
response.setContentType(text/html);
os = response.getOutputStream();
int num;
byte buf[] = new byte[1024];
while( (num=is.read(buf))!=-1 ){
os.write(buf, 0, num);
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
is.close();
os.close();
rs.close();
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
在页面中:
%
String path = request.getContextPath();
String basePath = request.getScheme()+://+request.getServerName()+:+request.getServerPort()+path+/;
%
img name=pic src=%=basePath+servlet/DownloadAsStream%/
搞定。
来源:(/s/blog_556c72d20100ejtw.html) - JAVA读取Oracle中的blob图片字段并显示_〓帅_新浪博客整个流程分为四步,连接oracle数据库 - 读取blob图片字段 - 对图片进行缩放 -把图片展示在jsp页面上。import java.sql.*;
import java.io.*;
?
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.awt.image.AffineTransformOp;
import java.awt.geom.AffineTransform;
?
public class OracleQueryBean {
??? private final String oracleDriverName = oracle.jdbc.driver.OracleDriver;
??? private Connection myConnection = null;
???
??? private String strTabName;
???
??? private String strIDName;
??
??? private String strImgName;
???
??? public OracleQueryBean(){
??????? try{
??????????? Class.forName(oracleDriverName);
??????? }catch(ClassNotFoundException ex){
??????????? System.out.println(加载jdbc驱动失败,原因: + ex.getMessage());
??????? }
??? }
???
??? public Connection getConnection(){
??????? try{
??????? //用户名+密码; 以下使用的Test就是Oracle里的表空间
??????? //从配置文件中读取数据库信息
??????? GetPara oGetPara = new GetPara();
??????? String strIP = oGetPara.getPara(serverip);
???????
显示全部