java oracle 图片_JAVA读取Oracle中的blob图片字段并显示
JAVA讀取Oracle中的blob圖片字段并顯示
近期,在給客戶做一個(gè)Demo頁(yè)面時(shí),需要用JAVA讀取Oracle中的blob圖片字段并顯示,在此過(guò)程中,遇到一些問(wèn)題,例如:連接Oracle數(shù)據(jù)庫(kù)讀取blob字段數(shù)據(jù),對(duì)圖片byte數(shù)據(jù)進(jìn)行縮放等;特此記錄,給自己備忘,給大家參考。
整個(gè)流程分為四步,連接oracle數(shù)據(jù)庫(kù)->讀取blob圖片字段->對(duì)圖片進(jìn)行縮放->把圖片展示在jsp頁(yè)面上。
下面進(jìn)行詳細(xì)描述:
1.java連接Oracle
注:數(shù)據(jù)庫(kù)是Oracle10g版本為10.2.0,在數(shù)據(jù)庫(kù)中,圖片字段類(lèi)型為BLOB。
java中通常使用的是通過(guò)jdbc驅(qū)動(dòng)來(lái)連接數(shù)據(jù)庫(kù),oracle也不例外,因此必須下載一個(gè)Oracle驅(qū)動(dòng)的jdbc需要去網(wǎng)上進(jìn)行下載,名稱(chēng)為ojdbc14.jar。
下載地址為:
下載了驅(qū)動(dòng)之后,可以使用驅(qū)動(dòng)里提供的接口進(jìn)行連接,具體代碼如下:
importjava.sql.*;
importjava.io.*;
importjavax.imageio.ImageIO;
importjava.awt.image.BufferedImage;
importjava.awt.image.AffineTransformOp;
importjava.awt.geom.AffineTransform;
publicclassOracleQueryBean {
privatefinalStringoracleDriverName="oracle.jdbc.driver.OracleDriver";
privateConnectionmyConnection=null;
/*圖片表名*/
privateStringstrTabName;
/*圖片ID字段名*/
privateStringstrIDName;
/*圖片字段名*/
privateStringstrImgName;
/**
*加載java連接Oracle的jdbc驅(qū)動(dòng)
*/
publicOracleQueryBean(){
try{
Class.forName(oracleDriverName);
}catch(ClassNotFoundException ex){
System.out.println("加載jdbc驅(qū)動(dòng)失敗,原因:"+ ex.getMessage());
}
}
/**
*獲取Oracle連接對(duì)象
*@returnConnection
*/
publicConnection getConnection(){
try{
//用戶名+密碼;以下使用的Test就是Oracle里的表空間
//從配置文件中讀取數(shù)據(jù)庫(kù)信息
GetPara
oGetPara =newGetPara();
String
strIP = oGetPara.getPara("serverip");
String
strPort = oGetPara.getPara("port");
String
strDBName = oGetPara.getPara("dbname");
String
strUser = oGetPara.getPara("user");
String
strPassword = oGetPara.getPara("password");
this.strTabName=
oGetPara.getPara("tablename");
this.strIDName=
oGetPara.getPara("imgidname");
this.strImgName=
oGetPara.getPara("imgname");
String
oracleUrlToConnect ="jdbc:oracle:thin:@"+strIP+":"+strPort+":"+strDBName;
this.myConnection= DriverManager.getConnection(oracleUrlToConnect,
strUser, strPassword);
}catch(Exception ex){
System.out.println("Can not get connection:"+ ex.getMessage());
System.out.println("請(qǐng)檢測(cè)配置文件中的數(shù)據(jù)庫(kù)信息是否正確.");
}
returnthis.myConnection;
}
}
2.讀取blob字段
在OracleQueryBean類(lèi)中增加一個(gè)函數(shù),來(lái)進(jìn)行讀取,具體代碼如下:
/**
*根據(jù)圖片在數(shù)據(jù)庫(kù)中的ID進(jìn)行讀取
*@paramstrID圖片字段ID
*@paramw需要縮到的寬度
*@paramh需要縮到高度
*@return
*/
publicbyte[]
GetImgByteById(String strID,intw,inth){
//System.out.println("Get img data which id is
" + nID);
if(myConnection==null)
this.getConnection();
byte[] data =null;
try{
Statement stmt =myConnection.createStatement();
ResultSet myResultSet = stmt.executeQuery("select "+this.strIDName+" from "+this.strTabName+" where "+this.strIDName+"="+ strID);
StringBuffer myStringBuffer =newStringBuffer();
if(myResultSet.next()) {
java.sql.Blob blob = myResultSet.getBlob(this.strImgName);
InputStream inStream = blob.getBinaryStream();
try{
longnLen = blob.length();
intnSize = (int) nLen;
//System.out.println("img
data size is :" + nSize);
data =newbyte[nSize];
inStream.read(data);
inStream.close();
}catch(IOException e) {
System.out.println("獲取圖片數(shù)據(jù)失敗,原因:"+ e.getMessage());
}
data = ChangeImgSize(data, w, h);
}
System.out.println(myStringBuffer.toString());
myConnection.commit();
myConnection.close();
}catch(SQLException ex) {
System.out.println(ex.getMessage());
}
returndata;
}
3.縮放圖片
因?yàn)閳D片的大小可能不一致,但是在頁(yè)面中輸出的大小需要統(tǒng)一,所以需要
在OracleQueryBean類(lèi)中增加一個(gè)函數(shù),來(lái)進(jìn)行縮放,具體代碼如下:
/**
*縮小或放大圖片
*@paramdata圖片的byte數(shù)據(jù)
*@paramw需要縮到的寬度
*@paramh需要縮到高度
*@return縮放后的圖片的byte數(shù)據(jù)
*/
privatebyte[] ChangeImgSize(byte[] data,intnw,intnh){
byte[] newdata =null;
try{
BufferedImage
bis = ImageIO.read(newByteArrayInputStream(data));
intw = bis.getWidth();
inth = bis.getHeight();
doublesx = (double) nw / w;
doublesy = (double) nh / h;
AffineTransform transform =newAffineTransform();
transform.setToScale(sx, sy);
AffineTransformOp ato =newAffineTransformOp(transform,null);
//原始顏色
BufferedImage bid =newBufferedImage(nw, nh, BufferedImage.TYPE_3BYTE_BGR);
ato.filter(bis, bid);
//轉(zhuǎn)換成byte字節(jié)
ByteArrayOutputStream baos =newByteArrayOutputStream();
ImageIO.write(bid,"jpeg", baos);
newdata = baos.toByteArray();
}catch(IOException
e){
e.printStackTrace();
}
returnnewdata;
}
4.展示在頁(yè)面
頁(yè)面使用OracleQueryBean來(lái)根據(jù)用戶提供的圖片id進(jìn)行查詢,在讀取并進(jìn)行縮放后,通過(guò)jsp頁(yè)面進(jìn)行展示,具體代碼如下:
response.setContentType("image/jpeg");
//圖片在數(shù)據(jù)庫(kù)中的ID
String strID = request.getParameter("id");
//要縮略或放大圖片的寬度
String strWidth = request.getParameter("w");
//要縮略或放大圖片的高度
String strHeight = request.getParameter("h");
byte[] data =null;
if(strID !=null){
intnWith = Integer.parseInt(strWidth);
intnHeight = Integer.parseInt(strHeight);
//獲取圖片的byte數(shù)據(jù)
data = OrcleQuery.GetImgByteById(strID, nWith, nHeight);
ServletOutputStream op = response.getOutputStream();
op.write(data, 0, data.length);
op.close();
op =null;
response.flushBuffer();
//清除輸出流,防止釋放時(shí)被捕獲異常
out.clear();
out =
pageContext.pushBody();
}
%>
5.OracleQueryBean查詢類(lèi)的整體代碼
OracleQueryBean.java文件代碼如下所示:
importjava.sql.*;
importjava.io.*;
importjavax.imageio.ImageIO;
importjava.awt.image.BufferedImage;
importjava.awt.image.AffineTransformOp;
importjava.awt.geom.AffineTransform;
publicclassOracleQueryBean {
privatefinalStringoracleDriverName="oracle.jdbc.driver.OracleDriver";
privateConnectionmyConnection=null;
/*圖片表名*/
privateStringstrTabName;
/*圖片ID字段名*/
privateStringstrIDName;
/*圖片字段名*/
privateStringstrImgName;
/**
*加載java連接Oracle的jdbc驅(qū)動(dòng)
*/
publicOracleQueryBean(){
try{
Class.forName(oracleDriverName);
}catch(ClassNotFoundException ex){
System.out.println("加載jdbc驅(qū)動(dòng)失敗,原因:"+ ex.getMessage());
}
}
/**
*獲取Oracle連接對(duì)象
*@returnConnection
*/
publicConnection getConnection(){
try{
//用戶名+密碼;以下使用的Test就是Oracle里的表空間
//從配置文件中讀取數(shù)據(jù)庫(kù)信息
GetPara
oGetPara =newGetPara();
String
strIP = oGetPara.getPara("serverip");
String
strPort = oGetPara.getPara("port");
String
strDBName = oGetPara.getPara("dbname");
String
strUser = oGetPara.getPara("user");
String
strPassword = oGetPara.getPara("password");
this.strTabName=
oGetPara.getPara("tablename");
this.strIDName=
oGetPara.getPara("imgidname");
this.strImgName=
oGetPara.getPara("imgname");
String
oracleUrlToConnect ="jdbc:oracle:thin:@"+strIP+":"+strPort+":"+strDBName;
this.myConnection= DriverManager.getConnection(oracleUrlToConnect,
strUser, strPassword);
}catch(Exception ex){
System.out.println("Can not get connection:"+ ex.getMessage());
System.out.println("請(qǐng)檢測(cè)配置文件中的數(shù)據(jù)庫(kù)信息是否正確.");
}
returnthis.myConnection;
}
/**
*根據(jù)圖片在數(shù)據(jù)庫(kù)中的ID進(jìn)行讀取
*@paramstrID圖片字段ID
*@paramw需要縮到的寬度
*@paramh需要縮到高度
*@return縮放后的圖片的byte數(shù)據(jù)
*/
publicbyte[] GetImgByteById(String strID,intw,inth){
//System.out.println("Get img data which id is
" + nID);
if(myConnection==null)
this.getConnection();
byte[] data =null;
try{
Statement stmt =myConnection.createStatement();
ResultSet myResultSet = stmt.executeQuery("select "+this.strIDName+" from "+this.strTabName+" where "+this.strIDName+"="+ strID);
StringBuffer myStringBuffer =newStringBuffer();
if(myResultSet.next()) {
java.sql.Blob blob = myResultSet.getBlob(this.strImgName);
InputStream inStream = blob.getBinaryStream();
try{
longnLen = blob.length();
intnSize = (int) nLen;
//System.out.println("img
data size is :" + nSize);
data =newbyte[nSize];
inStream.read(data);
inStream.close();
}catch(IOException e) {
System.out.println("獲取圖片數(shù)據(jù)失敗,原因:"+ e.getMessage());
}
data = ChangeImgSize(data, w, h);
}
System.out.println(myStringBuffer.toString());
myConnection.commit();
myConnection.close();
}catch(SQLException ex) {
System.out.println(ex.getMessage());
}
returndata;
}
/**
*根據(jù)圖片在數(shù)據(jù)庫(kù)中的ID進(jìn)行讀取,顯示原始大小的圖片
*@paramstrID圖片字段ID
*@return讀取后的圖片byte數(shù)據(jù)
*/
publicbyte[] GetImgByteById(String strID){
//System.out.println("Get img data which id is
" + nID);
if(myConnection==null)
this.getConnection();
byte[] data =null;
try{
Statement stmt =myConnection.createStatement();
ResultSet myResultSet = stmt.executeQuery("select "+this.strIDName+" from "+this.strTabName+" where "+this.strIDName+"="+ strID);
StringBuffer myStringBuffer =newStringBuffer();
if(myResultSet.next()) {
java.sql.Blob blob = myResultSet.getBlob(this.strImgName);
InputStream inStream = blob.getBinaryStream();
try{
longnLen = blob.length();
intnSize = (int) nLen;
data =newbyte[nSize];
inStream.read(data);
inStream.close();
}catch(IOException e) {
System.out.println("獲取圖片數(shù)據(jù)失敗,原因:"+ e.getMessage());
}
}
System.out.println(myStringBuffer.toString());
myConnection.commit();
myConnection.close();
}catch(SQLException ex) {
System.out.println(ex.getMessage());
}
returndata;
}
/**
*縮小或放大圖片
*@paramdata圖片的byte數(shù)據(jù)
*@paramw需要縮到的寬度
*@paramh需要縮到高度
*@return
*/
privatebyte[] ChangeImgSize(byte[] data,intnw,intnh){
byte[] newdata =null;
try{
BufferedImage
bis = ImageIO.read(newByteArrayInputStream(data));
intw = bis.getWidth();
inth = bis.getHeight();
doublesx = (double) nw / w;
doublesy = (double) nh / h;
AffineTransform transform =newAffineTransform();
transform.setToScale(sx, sy);
AffineTransformOp ato =newAffineTransformOp(transform,null);
//原始顏色
BufferedImage bid =newBufferedImage(nw, nh, BufferedImage.TYPE_3BYTE_BGR);
ato.filter(bis, bid);
//轉(zhuǎn)換成byte字節(jié)
ByteArrayOutputStream baos =newByteArrayOutputStream();
ImageIO.write(bid,"jpeg", baos);
newdata = baos.toByteArray();
}catch(IOException
e){
e.printStackTrace();
}
returnnewdata;
}
}
總結(jié)
以上是生活随笔為你收集整理的java oracle 图片_JAVA读取Oracle中的blob图片字段并显示的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: java构造顺序_Java构造顺序
- 下一篇: java中循环语句_Java语法基础之循