在Oracle中存取BLOB對象實現(xiàn)文件的上傳和下載
轉(zhuǎn)載于http://www.iocblog.net/java/j2ee/j2ee-oracle-blob.html最近做一個j2ee項目,需要在jsp頁面實現(xiàn)對文件的上傳和下載。很早以前就知道jdbc支持大對象(lob)的存取,以為很容易,做起來才發(fā)現(xiàn)問題多 多,讀了一大堆文章,反而沒有什么頭緒了。正如一位網(wǎng)友文章所講:“…網(wǎng)絡(luò)上的教程99%都是行不通的,連sun自己的文檔都一直錯誤……”,實際情況大 致如此了。
存取blob出現(xiàn)這么多問題,我認(rèn)為大半是由數(shù)據(jù)庫開發(fā)商、應(yīng)用服務(wù)器商在jdbc驅(qū)動上的不兼容性帶來的。而實際應(yīng)用中,每個人的開發(fā)運行環(huán)境不 同,使得某個網(wǎng)友的solution沒有辦法在別人的應(yīng)用中重現(xiàn),以至于罵聲一片。至于為什么會不兼容、有哪些問題,我沒有時間去弄清,這里只說說我們怎 樣解決了問題的。
基于上述原因,先列出我們的開發(fā)環(huán)境,免得有人配不出來,招人唾罵。
數(shù)據(jù)庫 oracle 9i
應(yīng)用服務(wù)器 bea weblogic 8.11
開發(fā)工具 jbuilder x
在jsp實現(xiàn)文件upload/download可以分成這樣幾塊 :文件提交到形成inputsteam;inputsteam以blob格式入庫;數(shù)據(jù)從庫中讀出為inputsteam;inputstream輸出到頁面形成下載文件。先說blob吧。
1.blob入庫
(1)直接獲得數(shù)據(jù)庫連接的情況
這是oracle提供的標(biāo)準(zhǔn)方式,先插入一個空blob對象,然后update這個空對象。代碼如下:
//得到數(shù)據(jù)庫連接(驅(qū)動包是weblogic的,沒有下載任何新版本)
class.forname("oracle.jdbc.driver.oracledriver");
connection con = drivermanager.getconnection(
"jdbc:oracle:thin:@localhost:1521:testdb", "test", "test");
//處理事務(wù)
con.setautocommit(false);
statement st = con.createstatement();
//插入一個空對象
st.executeupdate("insert into blobimg values(103,empty_blob())");
//用for update方式鎖定數(shù)據(jù)行
resultset rs = st.executequery(
"select contents from blobimg where id=103 for update");
if (rs.next()) {
//得到j(luò)ava.sql.blob對象,然后cast為oracle.sql.blob
oracle.sql.blob blob = (oracle.sql.blob) rs.getblob(1).;
//到數(shù)據(jù)庫的輸出流
outputstream outstream = blob.getbinaryoutputstream();
//這里用一個文件模擬輸入流
file file = new file("d:"proxy.txt");
inputstream fin = new fileinputstream(file);
//將輸入流寫到輸出流
byte[] b = new byte[blob.getbuffersize()];
int len = 0;
while ( (len = fin.read(b)) != -1) {
outstream.write(b, 0, len);
//blob.putbytes(1,b);
}
//依次關(guān)閉(注意順序)
fin.close();
outstream.flush();
outstream.close();
con.commit();
con.close();
(2)通過jndi獲得數(shù)據(jù)庫連接
在weblogic中配置到oracle的jdbc connection pool和datasource,綁定到context中,假定綁定名為”orads”。
為了得到數(shù)據(jù)庫連接,做一個連接工廠,主要代碼如下:
context context = new initialcontext();
ds = (datasource) context.lookup("orads");
return ds.getconnection();
以下是blob寫入數(shù)據(jù)庫的代碼:
connection con = connectionfactory.getconnection();
con.setautocommit(false);
statement st = con.createstatement();
st.executeupdate("insert into blobimg values(103,empty_blob())");
resultset rs = st.executequery(
"select contents from blobimg where id=103 for update");
if (rs.next()) {
//上面代碼不變
//這里不能用oracle.sql.blob,會報classcast 異常
weblogic.jdbc.vendor.oracle.oraclethinblobblob = (weblogic.jdbc.vendor.oracle.oraclethinblob) rs.getblob(1);
//以后代碼也不變
outputstream outstream = blob.getbinaryoutputstream();
file file = new file("d:"proxy.txt");
inputstream fin = new fileinputstream(file);
byte[] b = new byte[blob.getbuffersize()];
int len = 0;
while ( (len = fin.read(b)) != -1) {
outstream.write(b, 0, len);
}
fin.close();
outstream.flush();
outstream.close();
con.commit();
con.close();
2.blob出庫
從數(shù)據(jù)庫中讀出blob數(shù)據(jù)沒有上述由于連接池的不同帶來的差異,只需要j2se的標(biāo)準(zhǔn)類java.sql.blob就可以取得輸出流(注意區(qū)別java.sql.blob和oracle.sql.blob)。代碼如下:
connection con = connectionfactory.getconnection();
con.setautocommit(false);
statement st = con.createstatement();
//這里的sql語句不再需要”for update”
resultset rs = st.executequery(
"select contents from blobimg where id=103 ");
if (rs.next()) {
java.sql.blob blob = rs.getblob(1);
inputstream ins = blob.getbinarystream();
//用文件模擬輸出流
file file = new file("d:"output.txt");
outputstream fout = new fileoutputstream(file);
//下面將blob數(shù)據(jù)寫入文件
byte[] b = new byte[1024];
int len = 0;
while ( (len = ins.read(b)) != -1) {
fout.write(b, 0, len);
}
//依次關(guān)閉
fout.close();
ins.close();
con.commit();
con.close();
3.從jsp頁面提交文件到數(shù)據(jù)庫
(1)提交頁面的代碼如下:
<form action="handle.jsp" enctype="multipart/form-data" method="post" >
<input type="hidden" name="id" value="103"/>
<input type="file" name="filetoupload">
<input type="submit" value="upload">
</form>
(2)由于jsp沒有提供文件上傳的處理能力,只有使用第三方的開發(fā)包。網(wǎng)絡(luò)上開源的包有很多,我們這里選擇apache jakarta的fileupload,在http: //jakarta.apache.org/commons/fileupload/index.html 可以得到下載包和完整的api文檔。法奧為adajspexception
處理頁面(handle.jsp)的代碼如下
<%
boolean ismultipart = fileupload.ismultipartcontent(request);
if (ismultipart) {
// 建立一個新的upload對象
diskfileupload upload = new diskfileupload();
// 設(shè)置上載文件的參數(shù)
//upload.setsizethreshold(yourmaxmemorysize);
//upload.setsizemax(yourmaxrequestsize);
string rootpath = getservletconfig().getservletcontext().getrealpath("/") ;
upload.setrepositorypath(rootpath+""uploads");
// 分析request中的傳來的文件流,返回item的集合,
// 輪詢items,如果不是表單域,就是一個文件對象。
list items = upload.parserequest(request);
iterator iter = items.iterator();
while (iter.hasnext()) {
fileitem item = (fileitem) iter.next();
//如果是文件對象
if (!item.isformfield()) {
//如果是文本文件,可以直接顯示
//out.println(item.getstring());
//將上載的文件寫到服務(wù)器的web-infwebstart下,文件名為test.txt
//file uploadedfile = new file(rootpath+""uploads"test.txt");
//item.write(uploadedfile);
//下面的代碼是將文件入庫(略):
//注意輸入流的獲取
…
inputstream uploadedstream = item.getinputstream();
…
}
//否則是普通表單
else{
out.println("fieldname: " + item.getfieldname()+"<br>");
out.println("value: "+item.getstring()+"<br>"); }
}
}
%>
4.從數(shù)據(jù)庫讀取blob然后保存到客戶端磁盤上
這段代碼有點詭異,執(zhí)行后將會彈出文件保存對話窗口,將blob數(shù)據(jù)讀出保存到本地
posted on 2008-09-04 21:26 魯勝迪 閱讀(1556) 評論(0) 編輯 收藏 所屬分類: 一點點