posts - 64,  comments - 9,  trackbacks - 0
          Oracle中插入圖片并顯示(用BLOB類型)
          要在oracle里面存入圖片 用 blob類型


          首先在數(shù)據(jù)庫(kù)里建立:

          --連接到管理員
          conn sys/tbsoft as sysdba;

          --為scott用戶授權(quán)

          grant create any directory to scott;

          --回到scott用戶

          conn scott/tiger;


          --創(chuàng)建存儲(chǔ)圖片的表
          CREATE TABLE IMAGE_LOB (T_ID VARCHAR2 (5) NOT NULL,T_IMAGE BLOB NOT NULL);

          --創(chuàng)建存儲(chǔ)圖片的目錄
          CREATE OR REPLACE DIRECTORY IMAGES AS 'C:\picture';

          --在c:下自己建一個(gè)叫picture的文件夾

          CREATE OR REPLACE PROCEDURE IMG_INSERT (TID VARCHAR2,FILENAME VARCHAR2) AS
          F_LOB BFILE;--文件類型
          B_LOB BLOB;
          BEGIN
          iNSERT INTO IMAGE_LOB (T_ID, T_IMAGE)
          VALUES (TID,EMPTY_BLOB ()) RETURN T_IMAGE INTO B_LOB;
          --插入空的blob
          F_LOB:= BFILENAME ('IMAGES', FILENAME);
          --獲取指定目錄下的文件
          DBMS_LOB.FILEOPEN(F_LOB, DBMS_LOB.FILE_READONLY);
          --以只讀的方式打開文件
          DBMS_LOB.LOADFROMFILE (B_LOB, F_LOB,DBMS_LOB.GETLENGTH (F_LOB));
          --傳遞對(duì)象
          DBMS_LOB.FILECLOSE (F_LOB);
          --關(guān)閉原始文件
          COMMIT;
          END;
          /


          --在C:\picture下放一張圖片1.gif

          --將該圖片存入表
          call IMG_INSERT('1','1.gif');


          然后創(chuàng)建一個(gè)web項(xiàng)目 連接數(shù)據(jù)庫(kù)后 創(chuàng)建一個(gè)BlobDAO類 用來取出表中的blob類型圖片

          Java代碼 復(fù)制代碼
          1. public class BlobDAO {   
          2.   
          3.     private static final BlobDAO instance = new BlobDAO();   
          4.   
          5.     private Connection conn = null;   
          6.   
          7.     private BlobDAO() {   
          8.   
          9.     }   
          10.   
          11.     public static BlobDAO getInstance() {   
          12.         return instance;   
          13.     }   
          14.   
          15.     private void initConn() {   
          16.         conn = DBAccess.getInstance().getConn();   
          17.     }   
          18.   
          19.        
          20.     public byte[] getImage(String imgname) {   
          21.         BufferedInputStream ins;//取得BLOB的IO流   
          22.         byte[] bt = null;   
          23.   
          24.         initConn();   
          25.         Blob bo = null;   
          26.         PreparedStatement ps = null;   
          27.         ResultSet rs = null;   
          28.         String sql = "select T_IMAGE from IMAGE_LOB where t_id=?";   
          29.         try {   
          30.               ps = conn.prepareStatement(sql);   
          31.               ps.setString(1, imgname);   
          32.               rs = ps.executeQuery();   
          33.               if (rs.next()) {   
          34.                   bo = rs.getBlob("T_IMAGE");   
          35.   
          36.                   try {   
          37.                       ins = new BufferedInputStream(bo.getBinaryStream());   
          38.                       int bufferSize = (int) bo.length();//取得BLOB的長(zhǎng)度   
          39.                       bt = new byte[bufferSize];   
          40.                       try {   
          41.                             ins.read(bt, 0, bufferSize);   
          42.                       } catch (IOException e) {   
          43.                             // TODO Auto-generated catch block   
          44.                             e.printStackTrace();   
          45.                       }   
          46.                       //建立字節(jié)緩存   
          47.                   } catch (SQLException e) {   
          48.                       // TODO Auto-generated catch block   
          49.                       e.printStackTrace();   
          50.                   }   
          51.   
          52.               }   
          53.         } catch (SQLException e) {   
          54.               // TODO Auto-generated catch block   
          55.               e.printStackTrace();   
          56.         } finally {   
          57.               try {   
          58.                   rs.close();   
          59.                   ps.close();   
          60.                   conn.close();   
          61.               } catch (SQLException e) {   
          62.                   // TODO Auto-generated catch block   
          63.                   e.printStackTrace();   
          64.               }   
          65.         }   
          66.   
          67.         return bt;   
          68.     }   
          69. }  


          在action里面調(diào)用getImage()方法并顯示圖片在頁(yè)面上

          Java代碼 復(fù)制代碼
          1. public ActionForward execute(ActionMapping mapping, ActionForm form,   
          2.               HttpServletRequest request, HttpServletResponse response) {   
          3.         // TODO Auto-generated method stub   
          4.            
          5.         BlobDAO blobDAO = BlobDAO.getInstance();   
          6.            
          7.         byte[] bs = blobDAO.getImage("1");   
          8.            
          9.         try {   
          10.                  
          11.               response.getOutputStream().write(bs);   
          12.                  
          13.         } catch (IOException e) {   
          14.               // TODO Auto-generated catch block   
          15.               e.printStackTrace();   
          16.         }   
          17.            
          18.         return null;   
          19.     }  

          添加圖片到數(shù)據(jù)庫(kù)

          請(qǐng)?jiān)赾盤下放入圖片--c:\\4.gif

          Java代碼 復(fù)制代碼
          1. public void savaImg(String imgId) {   
          2.            //傳的是存入數(shù)據(jù)庫(kù)圖片的id   
          3.            initConn();   
          4.            Statement st = null;   
          5.            BLOB blob = null//圖片類型   
          6.            OutputStream outputStream = null//輸出流   
          7.            File file = null//文件   
          8.            InputStream inputStream = null//輸入流   
          9.            ResultSet rs = null;   
          10.            try {   
          11.                  conn.setAutoCommit(false); //事物由程序員操作   
          12.                  st = conn.createStatement();   
          13.                  st.executeQuery("insert into IMAGE_LOB values('"+ imgId +"',empty_blob())");   
          14.                  rs = st.executeQuery("select T_IMAGE from IMAGE_LOB where t_id='"+ imgId +"' for update");   
          15.                  if (rs.next()) {   
          16.                        blob = (BLOB) rs.getBlob(1);   
          17.                        outputStream = blob.getBinaryOutputStream();   
          18.                        file = new File("c:\\4.gif");   
          19.                        inputStream = new FileInputStream(file);   
          20.                        byte[] b = new byte[blob.getBufferSize()];   
          21.                        int len = 0;   
          22.                        while ((len = inputStream.read(b)) != -1) {   
          23.                              System.out.println(len);   
          24.                              outputStream.write(b, 0, len);   
          25.                        }   
          26.                  }   
          27.   
          28.            } catch (SQLException e) {   
          29.                  // TODO Auto-generated catch block   
          30.                  e.printStackTrace();   
          31.            } catch (FileNotFoundException e) {   
          32.                  // TODO Auto-generated catch block   
          33.                  e.printStackTrace();   
          34.            } catch (IOException e) {   
          35.                  // TODO Auto-generated catch block   
          36.                  e.printStackTrace();   
          37.            } finally {   
          38.                  try {   
          39.                        inputStream.close();   
          40.                        outputStream.flush();   
          41.                        outputStream.close();   
          42.                        rs.close();   
          43.                        st.close();   
          44.                        conn.commit();   
          45.                        conn.close();   
          46.                  } catch (IOException e) {   
          47.                        // TODO Auto-generated catch block   
          48.                        e.printStackTrace();   
          49.                  } catch (SQLException e) {   
          50.                        // TODO Auto-generated catch block   
          51.                        e.printStackTrace();   
          52.                  }   
          53.   
          54.            }   
          55.      }  



          操作完畢!
          posted on 2010-02-05 11:26 super_nini 閱讀(1460) 評(píng)論(0)  編輯  收藏

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          <2010年2月>
          31123456
          78910111213
          14151617181920
          21222324252627
          28123456
          78910111213

          常用鏈接

          留言簿

          隨筆檔案

          文章檔案

          相冊(cè)

          搜索

          •  

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 克拉玛依市| 景泰县| 宁夏| 深泽县| 兴国县| 高台县| 云霄县| 色达县| 兰坪| 宜都市| 库伦旗| 德安县| 成武县| 桐柏县| 禹州市| 自治县| 周至县| 天柱县| 南平市| 遵化市| 昭通市| 晋江市| 且末县| 钦州市| 溆浦县| 灯塔市| 黎平县| 商城县| 镇原县| 都昌县| 汉源县| 乳源| 洞头县| 彭山县| 甘泉县| 库伦旗| 富裕县| 湖口县| 江门市| 华蓥市| 井研县|