




版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
講解Java從數(shù)據(jù)庫(kù)中讀取Blob對(duì)象圖片并顯示的方法范文本文實(shí)例講述了java從數(shù)據(jù)庫(kù)中讀取Blob對(duì)象圖片并顯示的辦法。分享給大家供大家參考。具體實(shí)現(xiàn)辦法如下:
第一種辦法:
大致辦法就是,從數(shù)據(jù)庫(kù)中讀出Blob的流來(lái),寫(xiě)到頁(yè)面中去:
復(fù)制代碼代碼如下:Connectionconn=onnection();
Stringsql="SELECTpictureFROMteacherWHEREid=1";
PreparedStatementps=null;
ResultSetrs=null;
InputStreamis=null;
OutputStreamos=null;
try{
ps=areStatement(sql);
rs=uteQuery();
if(()){
is=inaryStream(1);
}
ontentType("text/html");
os=utputStream();
intnum;
bytebuf[]=newbyte[1024];
while((num=(buf))!=-1){
e(buf,0,num);
}
}catch(SQLExceptione){
tStackTrace();
}
try{
e();
e();
e();
e();
}catch(SQLExceptione){
tStackTrace();
}
在頁(yè)面中:
復(fù)制代碼代碼如下:Stringpath=ontextPath();
StringbasePath=cheme()+"://"+erverName()+":"+erverPort()+path+"/";
%>
搞定。
第二種辦法:
整個(gè)流程分為四步,連接oracle數(shù)據(jù)庫(kù)->讀取blob圖片字段->對(duì)圖片進(jìn)行縮放->把圖片展示在jsp頁(yè)面上。
復(fù)制代碼代碼如下:import.*;
import.*;
importeIO;
importeredImage;
importneTransformOp;
importneTransform;
publicclassOracleQueryBean{
privatefinalStringoracleDriverName="leDriver";
privateConnectionmyConnection=null;
privateStringstrTabName;
privateStringstrIDName;
privateStringstrImgName;
publicOracleQueryBean(){
try{
ame(oracleDriverName);
}catch(ClassNotFoundExceptionex){
tln("加載jdbc驅(qū)動(dòng)失敗,原因:"+essage());
}
}
publicConnectiongetConnection(){
try{
//用戶(hù)名+密碼;下列使用的Test就是Oracle里的表空間
//從配置文件中讀取數(shù)據(jù)庫(kù)信息
GetParaoGetPara=newGetPara();
StringstrIP=ara("serverip");
StringstrPort=ara("port");
StringstrDBName=ara("dbname");
StringstrUser=ara("user");
StringstrPassword=ara("password");
abName=ara("tablename");
DName=ara("imgidname");
mgName=ara("imgname");
StringoracleUrlToConnect="jdbc:oracle:thin:@"+strIP+":"+strPort+":"+strDBName;
nnection=onnection(oracleUrlToConnect,strUser,strPassword);
}catch(Exceptionex){
tln("Cannotgetconnection:"+essage());
tln("請(qǐng)檢測(cè)配置文件中的.數(shù)據(jù)庫(kù)信息是否正確.");
}
returnnnection;
}
}
2.讀取blob字段
在OracleQueryBean類(lèi)中增加一個(gè)函數(shù),來(lái)進(jìn)行讀取,具體代碼如下:
復(fù)制代碼代碼如下:publicbyte[]GetImgByteById(StringstrID,intw,inth){
//tln("Getimgdatawhichidis"+nID);
if(myConnection==null)
onnection();
byte[]data=null;
try{
Statementstmt=teStatement();
ResultSetmyResultSet=uteQuery("select"+DName+"from"+abName+"where"+DName+"="+strID);
StringBuffermyStringBuffer=newStringBuffer();
if(()){
blob=lob(mgName);
InputStreaminStream=inaryStream();
try{
longnLen=th();
intnSize=(int)nLen;
//tln("imgdatasizeis:"+nSize);
data=newbyte[nSize];
(data);
e();
}catch(IOExceptione){
tln("獲取圖片數(shù)據(jù)失敗,原因:"+essage());
}
data=ChangeImgSize(data,w,h);
}
tln(ring());
it();
e();
}catch(SQLExceptionex){
tln(essage());
}
returndata;
}
3.縮放圖片
因?yàn)閳D片的大小可能不一致,但是在頁(yè)面中輸出的大小需要統(tǒng)一,所以需要
在OracleQueryBean類(lèi)中增加一個(gè)函數(shù),來(lái)進(jìn)行縮放,具體代碼如下:
復(fù)制代碼代碼如下:privatebyte[]ChangeImgSize(byte[]data,intnw,intnh){
byte[]newdata=null;
try{
BufferedImagebis=(newByteArrayInputStream(data));
intw=idth();
inth=eight();
doublesx=(double)nw/w;
doublesy=(double)nh/h;
AffineTransformtransform=newAffineTransform();
oScale(sx,sy);
AffineTransformOpato=newAffineTransformOp(transform,null);
//原始顏色
BufferedImagebid=newBufferedImage(nw,nh,_3BYTE_BGR);
er(bis,bid);
//轉(zhuǎn)換成byte字節(jié)
ByteArrayOutputStreambaos=newByteArrayOutputStream();
e(bid,"jpeg",baos);
newdata=teArray();
}catch(IOExceptione){
tStackTrace();
}
returnnewdata;
}
4.展示在頁(yè)面
頁(yè)面使用OracleQueryBean來(lái)根據(jù)用戶(hù)提供的圖片id進(jìn)行查詢(xún),在讀取并進(jìn)行縮放后,通過(guò)jsp頁(yè)面進(jìn)行展示,具體代碼如下:
復(fù)制代碼代碼如下:
response.setContentType("image/jpeg");
//圖片在數(shù)據(jù)庫(kù)中的ID
StringstrID=arameter("id");
//要縮略或放大圖片的寬度
StringstrWidth=arameter("w");
//要縮略或放大圖片的高度
StringstrHeight=arameter("h");
byte[]data=null;
if(strID!=null){
intnWith=eInt(strWidth);
intnHeight=eInt(strHeight);
//獲取圖片的byte數(shù)據(jù)
data=mgByteById(strID,nWith,nHeight);
ServletOutputStreamop=utputStream();
e(data,0,th);
e();
op=null;
hBuffer();
//去除輸出流,避免釋放時(shí)被捕獲異常
r();
out=Body();
}
%>
5.OracleQueryBean查詢(xún)類(lèi)的整體代碼
文件代碼如下所示:
復(fù)制代碼代碼如下:import.*;
import.*;
importeIO;
importeredImage;
importneTransformOp;
importneTransform;
publicclassOracleQueryBean{
privatefinalStringoracleDriverName="leDriver";
privateConnectionmyConnection=null;
privateStringstrTabName;
privateStringstrIDName;
privateStringstrImgName;
publicOracleQueryBean(){
try{
ame(oracleDriverName);
}catch(ClassNotFoundExceptionex){
tln("加載jdbc驅(qū)動(dòng)失敗,原因:"+essage());
}
}
publicConnectiongetConnection(){
try{
//用戶(hù)名+密碼;下列使用的Test就是Oracle里的表空間
//從配置文件中讀取數(shù)據(jù)庫(kù)信息
GetParaoGetPara=newGetPara();
StringstrIP=ara("serverip");
StringstrPort=ara("port");
StringstrDBName=ara("dbname");
StringstrUser=ara("user");
StringstrPassword=ara("password");
abName=ara("tablename");
DName=ara("imgidname");
mgName=ara("imgname");
StringoracleUrlToConnect="jdbc:oracle:thin:@"+strIP+":"+strPort+":"+strDBName;
nnection=onnection(oracleUrlToConnect,strUser,strPassword);
}catch(Exceptionex){
tln("Cannotgetconnection:"+essage());
tln("請(qǐng)檢測(cè)配置文件中的數(shù)據(jù)庫(kù)信息是否正確.");
}
returnnnection;
}
publicbyte[]GetImgByteById(StringstrID,intw,inth){
//tln("Getimgdatawhichidis"+nID);
if(myConnection==null)
onnection();
byte[]data=null;
try{
Statementstmt=teStatement();
ResultSetmyResultSet=uteQuery("select"+DName+"from"+abName+"where"+DName+"="+strID);
StringBuffermyStringBuffer=newStringBuffer();
if(()){
blob=lob(mgName);
InputStreaminStream=inaryStream();
try{
longnLen=th();
intnSize=(int)nLen;
//tln("imgdatasizeis:"+nSize);
data=newbyte[nSize];
(data);
e();
}catch(IOExceptione){
tln("獲取圖片數(shù)據(jù)失敗,原因:"+essage());
}
data=ChangeImgSize(data,w,h);
}
tln(ring());
it();
e();
}catch(SQLExceptionex){
tln(essage());
}
returndata;
}
publicbyte[]GetImgByteById(StringstrID){
//tln("Getimgdatawhichidis"+nID);
if(myConnection==null)
onnection();
byte[]data=null;
try{
Statementstmt=teStatement();
ResultSetmyResultSet=uteQuery("select"+DName+"from"+abName+"where"+DName+"="+strID);
StringBuffermyStringBuffer=newStringBuffer();
if(()){
blob=lob(mgName);
InputStreaminStream=inaryStream();
try{
longnLen=th();
intnSize=(int)nLen;
data=newbyte[nSize];
(data);
e();
}catch(IOExceptione){
tln("獲取圖片數(shù)據(jù)失敗,原因:"+essage());
}
}
tln(ring());
it();
e();
}catch(SQLExceptionex){
tln(essage());
}
returndata;
}
privatebyte[]ChangeImgSize(byte[]data,intnw,intnh){
byte[]newdata=null;
try{
BufferedImagebis=(newByteArrayInputStream(data));
intw=idth();
inth=eight();
doublesx=(double)nw/w;
doublesy=(double)nh/h;
AffineTransformtransform=newAffineTransform();
oScale(sx,sy);
AffineTransformOpato=newAffineTransformOp(transform,null);
//原始顏色
BufferedImagebid=newBufferedImage(nw,nh,_3BYTE_BGR);
er(bis,bid);
//轉(zhuǎn)換成byte字節(jié)
ByteArrayOutputStreambaos=newByteArrayOutputStream();
e(bid,"jpeg",baos);
newdata=teArray();
}catch(IOExceptione){
tStackTrace();
}
returnnewdata;
}
}
下面是我的存儲(chǔ)讀取blob圖片的案例
復(fù)制代碼代碼如下:import.*;
import.*;
publicclassInsertPhoto{
publicstaticvoidmain(String[]args)throwsException{
ame("er");
Connectioncon=onnection("jdbc:mysql://);
Filef=newFile("e:/123.jpg");
FileInputStreamfis=newFileInputStream(f);
Stringsql="intophoto(photo,photoName)values(";
PreparedStatementpstmt=areStatement(sql);
inaryStream(1,fis,(int)th());
tring(2,"測(cè)試圖片");
uteUpdate();
e();
e();
e();
}
}
復(fù)制代碼代碼如下:importeredImage;
importeredInputStream;
importception;
importtStream;
importutStream;
importection;
importerManager;
importltSet;
importxception;
importement;
importeIO;
importServlet;
importServletRequest;
importServletResponse;
importcom.sun.image.codec.jpeg.JPEGCodec;
importcom.sun.image.codec.jpeg.JPEGImageEncoder;
publicclassReadPhotoextendsHttpServlet{
privatestaticfinallongserialVersionUID=1L;
publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse){
if(arameter("id")!=null){
response.setContentType("image/jpeg");
try{
InputStreamis=query_getPhotoImageBlob(eInt(arameter("id")));
if(is!=null){
is=newBufferedInputStream(is);
BufferedImagebi=(is);
OutputStreamos=utputStream();
JPEGImageEncoderencoder=JPEGCodec.createJPEGEncoder(os
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 教育技術(shù)如何助力遠(yuǎn)程學(xué)習(xí)減少地域差異
- 教育行業(yè)大數(shù)據(jù)的安全與隱私保護(hù)
- 教育創(chuàng)新項(xiàng)目中的人文關(guān)懷與社會(huì)責(zé)任
- 2025秋冀教版2024七年級(jí)上冊(cè)英語(yǔ)課件 Unit 2 6. Lesson 6
- 2025湖南大眾傳媒職業(yè)技術(shù)學(xué)院?jiǎn)握小堵殬I(yè)適應(yīng)性測(cè)試》模擬試題附參考答案詳解(黃金題型)
- 山東公務(wù)員行測(cè)真題及答案
- 2024年北京大學(xué)第一醫(yī)院寧夏婦女兒童醫(yī)院招聘筆試真題
- 2024-2025學(xué)年度安徽交通職業(yè)技術(shù)學(xué)院?jiǎn)握小段锢怼房荚嚲C合練習(xí)一套附答案詳解
- 幼兒園世界脊柱健康日科普教育
- 2024年江蘇農(nóng)牧科技職業(yè)學(xué)院?jiǎn)握小墩Z(yǔ)文》能力檢測(cè)試卷及答案詳解(全優(yōu))
- DL∕T 901-2017 火力發(fā)電廠煙囪(煙道)防腐蝕材料
- DL∕T 664-2016 帶電設(shè)備紅外診斷應(yīng)用規(guī)范
- 河北省承德市平泉市2023-2024學(xué)年七年級(jí)下學(xué)期期末數(shù)學(xué)試題(無(wú)答案)
- DL-T448-2016電能計(jì)量裝置技術(shù)管理規(guī)程
- 2024建筑工程勞務(wù)分包合同標(biāo)準(zhǔn)范本
- QB/T 2660-2024 化妝水(正式版)
- 《化工和危險(xiǎn)化學(xué)品生產(chǎn)經(jīng)營(yíng)單位重大生產(chǎn)安全事故隱患判定標(biāo)準(zhǔn)(試行)》解讀課件
- 數(shù)學(xué)分析教學(xué)課件
- 基于Python+MySQL的員工管理系統(tǒng)的設(shè)計(jì)與實(shí)現(xiàn)
- 拔絲生產(chǎn)企業(yè)管理制度
- 可視對(duì)講及門(mén)禁的課程設(shè)計(jì)
評(píng)論
0/150
提交評(píng)論