<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    在Oracle中存取BLOB對(duì)象實(shí)現(xiàn)文件的上傳和下載

    轉(zhuǎn)載于http://www.iocblog.net/java/j2ee/j2ee-oracle-blob.html

    最近做一個(gè)j2ee項(xiàng)目,需要在jsp頁面實(shí)現(xiàn)對(duì)文件的上傳和下載。很早以前就知道jdbc支持大對(duì)象(lob)的存取,以為很容易,做起來才發(fā)現(xiàn)問題多 多,讀了一大堆文章,反而沒有什么頭緒了。正如一位網(wǎng)友文章所講:“…網(wǎng)絡(luò)上的教程99%都是行不通的,連sun自己的文檔都一直錯(cuò)誤……”,實(shí)際情況大 致如此了。

    存取blob出現(xiàn)這么多問題,我認(rèn)為大半是由數(shù)據(jù)庫開發(fā)商、應(yīng)用服務(wù)器商在jdbc驅(qū)動(dòng)上的不兼容性帶來的。而實(shí)際應(yīng)用中,每個(gè)人的開發(fā)運(yùn)行環(huán)境不 同,使得某個(gè)網(wǎng)友的solution沒有辦法在別人的應(yīng)用中重現(xiàn),以至于罵聲一片。至于為什么會(huì)不兼容、有哪些問題,我沒有時(shí)間去弄清,這里只說說我們?cè)? 樣解決了問題的。

    基于上述原因,先列出我們的開發(fā)環(huán)境,免得有人配不出來,招人唾罵。

    數(shù)據(jù)庫 oracle 9i

    應(yīng)用服務(wù)器 bea weblogic 8.11

    開發(fā)工具 jbuilder x

    在jsp實(shí)現(xiàn)文件upload/download可以分成這樣幾塊 :文件提交到形成inputsteam;inputsteam以blob格式入庫;數(shù)據(jù)從庫中讀出為inputsteam;inputstream輸出到頁面形成下載文件。先說blob吧。

    1.blob入庫
    (1)直接獲得數(shù)據(jù)庫連接的情況

    這是oracle提供的標(biāo)準(zhǔn)方式,先插入一個(gè)空blob對(duì)象,然后update這個(gè)空對(duì)象。代碼如下:

    //得到數(shù)據(jù)庫連接(驅(qū)動(dòng)包是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();

    //插入一個(gè)空對(duì)象

    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對(duì)象,然后cast為oracle.sql.blob

    oracle.sql.blob blob = (oracle.sql.blob) rs.getblob(1).;

    //到數(shù)據(jù)庫的輸出流

    outputstream outstream = blob.getbinaryoutputstream();

    //這里用一個(gè)文件模擬輸入流

    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ù)庫連接,做一個(gè)連接工廠,主要代碼如下:

    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,會(huì)報(bào)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) {

    // 建立一個(gè)新的upload對(duì)象

    diskfileupload upload = new diskfileupload();

    // 設(shè)置上載文件的參數(shù)

    //upload.setsizethreshold(yourmaxmemorysize);

    //upload.setsizemax(yourmaxrequestsize);

    string rootpath = getservletconfig().getservletcontext().getrealpath("/") ;

    upload.setrepositorypath(rootpath+""uploads");

    // 分析request中的傳來的文件流,返回item的集合,

    // 輪詢items,如果不是表單域,就是一個(gè)文件對(duì)象。

    list items = upload.parserequest(request);

    iterator iter = items.iterator();

    while (iter.hasnext()) {

    fileitem item = (fileitem) iter.next();

    //如果是文件對(duì)象

    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然后保存到客戶端磁盤上
    這段代碼有點(diǎn)詭異,執(zhí)行后將會(huì)彈出文件保存對(duì)話窗口,將blob數(shù)據(jù)讀出保存到本地

    posted on 2008-09-04 21:26 魯勝迪 閱讀(1556) 評(píng)論(0)  編輯  收藏 所屬分類: 一點(diǎn)點(diǎn)

    <2008年9月>
    31123456
    78910111213
    14151617181920
    21222324252627
    2829301234
    567891011

    導(dǎo)航

    統(tǒng)計(jì)

    常用鏈接

    留言簿(4)

    隨筆分類

    隨筆檔案

    文章分類

    新聞分類

    搜索

    最新評(píng)論

    閱讀排行榜

    評(píng)論排行榜

    主站蜘蛛池模板: 亚洲人成色7777在线观看不卡| 国产一卡2卡3卡4卡无卡免费视频| 国产成人精品高清免费| 亚洲无mate20pro麻豆| 国产男女爽爽爽爽爽免费视频 | 亚洲天堂在线视频| 一区二区免费电影| 国产成人精品日本亚洲专区 | 在线天堂免费观看.WWW| 精品国产成人亚洲午夜福利| 999国内精品永久免费观看| 亚洲一区精彩视频| 在线观看免费大黄网站| 国产亚洲福利精品一区二区| 免费人成激情视频| 成人毛片100免费观看| 亚洲天天做日日做天天看| 1000部免费啪啪十八未年禁止观看 | 亚洲乱码一二三四区麻豆| 国产精品免费观看久久| 亚洲av成人无码网站…| 亚洲AV无码乱码在线观看牲色| 18禁超污无遮挡无码免费网站| 亚洲精品国产电影午夜| 成人永久免费高清| 国产裸体美女永久免费无遮挡| 久久久久久亚洲Av无码精品专口| 免费影院未满十八勿进网站| 国产精品久久久久久亚洲影视| 久久久久亚洲爆乳少妇无 | 国产性生交xxxxx免费| 国产在线观a免费观看| 亚洲嫩草影院在线观看| 国产无遮挡裸体免费视频| a级毛片免费高清毛片视频| 亚洲国产精品成人久久久| 亚洲国产成人精品无码久久久久久综合| 另类免费视频一区二区在线观看 | 国产精品二区三区免费播放心 | 国产精品亚洲va在线观看| 亚洲国产精华液网站w|