Java从数据库中读取Blob对象图⽚并显⽰的⽅法本⽂实例讲述了Java从数据库中读取Blob对象图⽚并显⽰的⽅法。分享给⼤家供⼤家参考。具体实现⽅法如下:
第⼀种⽅法:
⼤致⽅法就是,从数据库中读出Blob的流来,写到页⾯中去:
复制代码代码如下:
Connection conn = Connection();
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();
()){
is = rs.getBinaryStream(1);
}
response.setContentType("text/html");
os = OutputStream();
int num;
byte buf[] = new byte[1024];
while( (ad(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 = ContextPath();
String basePath = Scheme()+"://"+ServerName()+":"+ServerPort()+path+"/"; %>
<img name="pic" src="<%=basePath+"servlet/DownloadAsStream"%>"/>
搞定。
第⼆种⽅法:
整个流程分为四步,连接oracle数据库 -> 读取blob图⽚字段 -> 对图⽚进⾏缩放 ->把图⽚展⽰在jsp页⾯上。
复制代码代码如下:
import java.sql.*;
import java.io.*;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.awt.image.AffineTransformOp;
import 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 = Para("serverip");
String strPort = Para("port");
String strDBName = Para("dbname");
String strUser = Para("user");
String strPassword = Para("password");
this.strTabName = Para("tablename");
this.strIDName = Para("imgidname");
this.strImgName = Para("imgname");
对象图片高清
String oracleUrlToConnect ="jdbc:oracle:thin:@"+strIP+":"+strPort+":"+strDBName;
System.out.println("Can not get connection:" + ex.getMessage());
System.out.println("请检测配置⽂件中的数据库信息是否正确." );
}
Connection;
}
}
2. 读取blob字段
在OracleQueryBean类中增加⼀个函数,来进⾏读取,具体代码如下:
复制代码代码如下:
public byte[] GetImgByteById(String strID, int w, int h){
//System.out.println("Get img data which id is " + nID);
if(myConnection == null)
byte[] data = null;
try {
Statement stmt = ateStatement();
ResultSet myResultSet = uteQuery("select " + this.strIDName + " from " + this.strTabName + " where " + this.strIDName + "=" + strID);
StringBuffer myStringBuffer = new StringBuffer();
if (()) {
java.sql.Blob blob = Blob(this.strImgName);
InputStream inStream = BinaryStream();
try {
long nLen = blob.length();
int nSize = (int) nLen;
//System.out.println("img data size is :" + nSize);
data = new byte[nSize];
inStream.close();
} catch (IOException e) {
System.out.println("获取图⽚数据失败,原因:" + e.getMessage());
}
data = ChangeImgSize(data, w, h);
}
System.out.String());
myConnectionmit();
myConnection.close();
} catch (SQLException ex) {
System.out.Message());
}
return data;
}
3. 缩放图⽚
因为图⽚的⼤⼩可能不⼀致,但是在页⾯中输出的⼤⼩需要统⼀,所以需要
在OracleQueryBean类中增加⼀个函数,来进⾏缩放,具体代码如下:
复制代码代码如下:
private byte[] ChangeImgSize(byte[] data, int nw, int nh){
byte[] newdata = null;
try{
BufferedImage bis = ad(new ByteArrayInputStream(data));
int w = Width();
int h = Height();
double sx = (double) nw / w;
double sy = (double) nh / h;
AffineTransform transform = new AffineTransform();
transform.setToScale(sx, sy);
AffineTransformOp ato = new AffineTransformOp(transform, null);
//原始颜⾊
BufferedImage bid = new BufferedImage(nw, nh, BufferedImage.TYPE_3BYTE_BGR);
ato.filter(bis, bid);
//转换成byte字节
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(bid, "jpeg", baos);
newdata = ByteArray();
}catch(IOException e){
e.printStackTrace();
}
return newdata;
}
4. 展⽰在页⾯
页⾯使⽤OracleQueryBean来根据⽤户提供的图⽚id进⾏查询,在读取并进⾏缩放后,通过jsp页⾯进⾏展⽰,具体代码如下:
复制代码代码如下:
<%@ page language="java" contentType="text/html;;charset=gbk" %>
<jsp:useBean id="OrcleQuery" scope="page" class="HLFtiDemo.OracleQueryBean" />
<%
response.setContentType("image/jpeg");
//图⽚在数据库中的 ID
String strID = Parameter("id");
//要缩略或放⼤图⽚的宽度
String strWidth = Parameter("w");
//要缩略或放⼤图⽚的⾼度
String strHeight = Parameter("h");
byte[] data = null;
if(strID != null){
int nWith = Integer.parseInt(strWidth);
int nHeight = Integer.parseInt(strHeight);
//获取图⽚的byte数据
data = OrcleQuery.GetImgByteById(strID, nWith, nHeight);
ServletOutputStream op = OutputStream();
op.write(data, 0, data.length);
op.close();
op = null;
response.flushBuffer();
//清除输出流,防⽌释放时被捕获异常
out.clear();
out = pageContext.pushBody();
}
%>
5. OracleQueryBean查询类的整体代码
OracleQueryBean.java⽂件代码如下所⽰:
复制代码代码如下:
import java.sql.*;
import java.io.*;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.awt.image.AffineTransformOp;
import 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 = Para("serverip");
String strPort = Para("port");
String strDBName = Para("dbname");
String strUser = Para("user");
String strPassword = Para("password");
this.strTabName = Para("tablename");
this.strIDName = Para("imgidname");
this.strImgName = Para("imgname");
String oracleUrlToConnect ="jdbc:oracle:thin:@"+strIP+":"+strPort+":"+strDBName;
}catch(Exception ex){
System.out.println("Can not get connection:" + ex.getMessage());
System.out.println("请检测配置⽂件中的数据库信息是否正确." );
}
Connection;
}
public byte[] GetImgByteById(String strID, int w, int h){
/
/System.out.println("Get img data which id is " + nID);
if(myConnection == null)
byte[] data = null;
try {
Statement stmt = ateStatement();
ResultSet myResultSet = uteQuery("select " + this.strIDName + " from " + this.strTabName + " where " + this.strIDName + "=" + strID);
StringBuffer myStringBuffer = new StringBuffer();
if (()) {
java.sql.Blob blob = Blob(this.strImgName);
InputStream inStream = BinaryStream();
try {
long nLen = blob.length();
int nSize = (int) nLen;
//System.out.println("img data size is :" + nSize);
data = new byte[nSize];
inStream.close();
} catch (IOException e) {
System.out.println("获取图⽚数据失败,原因:" + e.getMessage());
}
data = ChangeImgSize(data, w, h);
}
System.out.String());
myConnectionmit();
myConnection.close();
} catch (SQLException ex) {
System.out.Message());
}
return data;
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论