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

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

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

    在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)  編輯  收藏 所屬分類: 一點點

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

    導(dǎo)航

    統(tǒng)計

    常用鏈接

    留言簿(4)

    隨筆分類

    隨筆檔案

    文章分類

    新聞分類

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 亚洲黄色网址在线观看| 四虎在线成人免费网站| 亚洲卡一卡二卡乱码新区| 亚洲av永久无码精品秋霞电影影院| 免费无遮挡无码视频网站| **一级一级毛片免费观看| 日本道免费精品一区二区| 国产精品亚洲精品爽爽| 亚洲乱码卡一卡二卡三| 亚洲成人精品久久| 亚洲中文字幕无码不卡电影| 免费二级毛片免费完整视频| 免费看的一级毛片| 欧美在线看片A免费观看| 2020因为爱你带字幕免费观看全集| a级在线观看免费| 国产精品福利在线观看免费不卡| 免费高清A级毛片在线播放| 亚洲av无码日韩av无码网站冲| 亚洲成_人网站图片| 麻豆狠色伊人亚洲综合网站| 激情内射亚洲一区二区三区| 久久精品国产亚洲AV麻豆不卡| 在线观看国产区亚洲一区成人 | 亚洲精品无码不卡在线播HE| 亚洲精品天堂成人片?V在线播放| 免费真实播放国产乱子伦| 国产在线观看免费不卡| 国产伦精品一区二区三区免费下载| 高清国语自产拍免费视频国产| 午夜成人免费视频| 国产高清在线精品免费软件| 国产精品麻豆免费版| 亚洲成A人片77777国产| 亚洲日本一区二区三区在线不卡| 亚洲情侣偷拍精品| 亚洲中文久久精品无码| 亚洲av综合av一区| 亚洲最新永久在线观看| 亚洲AV无码久久久久网站蜜桃 | 国产免费av片在线无码免费看|