java oracle 图片_JAVA读取Oracle中的blob图片字段并显示
JAVA讀取Oracle中的blob圖片字段并顯示
近期,在給客戶做一個Demo頁面時,需要用JAVA讀取Oracle中的blob圖片字段并顯示,在此過程中,遇到一些問題,例如:連接Oracle數據庫讀取blob字段數據,對圖片byte數據進行縮放等;特此記錄,給自己備忘,給大家參考。
整個流程分為四步,連接oracle數據庫->讀取blob圖片字段->對圖片進行縮放->把圖片展示在jsp頁面上。
下面進行詳細描述:
1.java連接Oracle
注:數據庫是Oracle10g版本為10.2.0,在數據庫中,圖片字段類型為BLOB。
java中通常使用的是通過jdbc驅動來連接數據庫,oracle也不例外,因此必須下載一個Oracle驅動的jdbc需要去網上進行下載,名稱為ojdbc14.jar。
下載地址為:
下載了驅動之后,可以使用驅動里提供的接口進行連接,具體代碼如下:
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驅動
*/
publicOracleQueryBean(){
try{
Class.forName(oracleDriverName);
}catch(ClassNotFoundException ex){
System.out.println("加載jdbc驅動失敗,原因:"+ ex.getMessage());
}
}
/**
*獲取Oracle連接對象
*@returnConnection
*/
publicConnection getConnection(){
try{
//用戶名+密碼;以下使用的Test就是Oracle里的表空間
//從配置文件中讀取數據庫信息
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("請檢測配置文件中的數據庫信息是否正確.");
}
returnthis.myConnection;
}
}
2.讀取blob字段
在OracleQueryBean類中增加一個函數,來進行讀取,具體代碼如下:
/**
*根據圖片在數據庫中的ID進行讀取
*@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("獲取圖片數據失敗,原因:"+ 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.縮放圖片
因為圖片的大小可能不一致,但是在頁面中輸出的大小需要統一,所以需要
在OracleQueryBean類中增加一個函數,來進行縮放,具體代碼如下:
/**
*縮小或放大圖片
*@paramdata圖片的byte數據
*@paramw需要縮到的寬度
*@paramh需要縮到高度
*@return縮放后的圖片的byte數據
*/
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);
//轉換成byte字節
ByteArrayOutputStream baos =newByteArrayOutputStream();
ImageIO.write(bid,"jpeg", baos);
newdata = baos.toByteArray();
}catch(IOException
e){
e.printStackTrace();
}
returnnewdata;
}
4.展示在頁面
頁面使用OracleQueryBean來根據用戶提供的圖片id進行查詢,在讀取并進行縮放后,通過jsp頁面進行展示,具體代碼如下:
response.setContentType("image/jpeg");
//圖片在數據庫中的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數據
data = OrcleQuery.GetImgByteById(strID, nWith, nHeight);
ServletOutputStream op = response.getOutputStream();
op.write(data, 0, data.length);
op.close();
op =null;
response.flushBuffer();
//清除輸出流,防止釋放時被捕獲異常
out.clear();
out =
pageContext.pushBody();
}
%>
5.OracleQueryBean查詢類的整體代碼
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驅動
*/
publicOracleQueryBean(){
try{
Class.forName(oracleDriverName);
}catch(ClassNotFoundException ex){
System.out.println("加載jdbc驅動失敗,原因:"+ ex.getMessage());
}
}
/**
*獲取Oracle連接對象
*@returnConnection
*/
publicConnection getConnection(){
try{
//用戶名+密碼;以下使用的Test就是Oracle里的表空間
//從配置文件中讀取數據庫信息
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("請檢測配置文件中的數據庫信息是否正確.");
}
returnthis.myConnection;
}
/**
*根據圖片在數據庫中的ID進行讀取
*@paramstrID圖片字段ID
*@paramw需要縮到的寬度
*@paramh需要縮到高度
*@return縮放后的圖片的byte數據
*/
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("獲取圖片數據失敗,原因:"+ 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;
}
/**
*根據圖片在數據庫中的ID進行讀取,顯示原始大小的圖片
*@paramstrID圖片字段ID
*@return讀取后的圖片byte數據
*/
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("獲取圖片數據失敗,原因:"+ e.getMessage());
}
}
System.out.println(myStringBuffer.toString());
myConnection.commit();
myConnection.close();
}catch(SQLException ex) {
System.out.println(ex.getMessage());
}
returndata;
}
/**
*縮小或放大圖片
*@paramdata圖片的byte數據
*@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);
//轉換成byte字節
ByteArrayOutputStream baos =newByteArrayOutputStream();
ImageIO.write(bid,"jpeg", baos);
newdata = baos.toByteArray();
}catch(IOException
e){
e.printStackTrace();
}
returnnewdata;
}
}
總結
以上是生活随笔為你收集整理的java oracle 图片_JAVA读取Oracle中的blob图片字段并显示的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java构造顺序_Java构造顺序
- 下一篇: java resources 目录_Ma