• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            大龍的博客

            常用鏈接

            統計

            最新評論

            oracle blob數據存取

                Oracle中的lob (Large Object)可以存儲非常大的數據(可能是4GB),這樣就可以通過將文件或其它任何對象序列化成字節輸出流(OutputStream)后寫入數據 庫,之后使用字節輸入流(InputStream)將數據讀出然后反序列化為原始文件或對象。操作時需要使用oracle的JDBC包,它擴展了sun的 JDBC包中的Blob對象。
                以下是一個保存圖片進數據庫的例子:
            1.servlet:用于保存圖片并將圖片輸出
            Java代碼  收藏代碼
            1. package com.logcd.servlet;  
            2.   
            3. import java.io.IOException;  
            4. import java.io.InputStream;  
            5. import java.io.OutputStream;  
            6. import java.sql.CallableStatement;  
            7. import java.sql.Connection;  
            8. import java.sql.DriverManager;  
            9. import java.sql.ResultSet;  
            10. import java.sql.SQLException;  
            11. import java.sql.Statement;  
            12. import java.sql.Types;  
            13. import java.util.Iterator;  
            14. import java.util.List;  
            15.   
            16. import javax.servlet.ServletException;  
            17. import javax.servlet.http.HttpServlet;  
            18. import javax.servlet.http.HttpServletRequest;  
            19. import javax.servlet.http.HttpServletResponse;  
            20.   
            21. import oracle.sql.BLOB;  
            22.   
            23. import org.apache.commons.fileupload.FileItem;  
            24. import org.apache.commons.fileupload.FileUploadException;  
            25. import org.apache.commons.fileupload.disk.DiskFileItemFactory;  
            26. import org.apache.commons.fileupload.servlet.ServletFileUpload;  
            27.   
            28. public class ImageServlet extends HttpServlet {  
            29.   
            30.     private static final long serialVersionUID = 1L;  
            31.   
            32.     /** 
            33.      * 處理請求 
            34.      * @throws FileUploadException 
            35.      */  
            36.     public void doPost(HttpServletRequest request, HttpServletResponse response)  
            37.             throws ServletException, IOException {  
            38.   
            39.         Iterator<FileItem> i = getFileItem(request);  
            40.         String title = "";  
            41.         byte[] data = null;  
            42.   
            43.         while (i.hasNext()) {  
            44.             FileItem fi = (FileItem) i.next();  
            45.             if (fi.isFormField()) {// 取得表單域  
            46.                 if(fi.getFieldName().equalsIgnoreCase("title")){  
            47.                               title = new String(fi.getString().getBytes("iso8859-1"),"gbk");  
            48.                     }  
            49.             } else {// 取文件域  
            50.                 data = fi.get();//文件二進制數據  
            51.             }  
            52.         }  
            53.   
            54.         Integer id = saveImageUseProc(data,title);//saveImage(data, title);//存入   
            55.         //outputImage(response, id);//讀出  
            56.         outputImageUseProc(response,id);  
            57.     }  
            58.   
            59.     public void doGet(HttpServletRequest request, HttpServletResponse response)  
            60.             throws ServletException, IOException {  
            61.         doPost(request, response);  
            62.     }  
            63.   
            64.     /** 
            65.      * 通過SQL保存圖片 
            66.      * @param data 
            67.      * @param title 
            68.      */  
            69.     @SuppressWarnings("deprecation")  
            70.     public static Integer saveImage(byte[] data, String title) {  
            71.         Connection conn = getConnection();  
            72.         Integer id = (int) (Math.random() * 100000);  
            73.         String sql = "insert into t_image(id,title,image) values(" + id + ",'"  
            74.                 + title + "',empty_blob())";  
            75.         Statement stmt;  
            76.         OutputStream outStream = null;  
            77.         try {  
            78.             conn.setAutoCommit(false);// 如果不關閉會報-->"錯誤:讀取違反順序"  
            79.   
            80.             stmt = conn.createStatement();  
            81.             stmt.execute(sql);  
            82.   
            83.             String sqll = "select image from t_image where id=" + id  
            84.                     + " for update";  
            85.   
            86.             ResultSet rs = stmt.executeQuery(sqll);  
            87.             if (rs.next()) {  
            88.                 BLOB blob = (BLOB) rs.getBlob("image");  
            89.                 outStream = blob.getBinaryOutputStream();  
            90.                 // data是傳入的byte數組,定義:byte[] data  
            91.                 outStream.write(data, 0, data.length);  
            92.   
            93.                 outStream.flush();  
            94.                 outStream.close();  
            95.                 conn.commit();  
            96.             }  
            97.         } catch (Exception e) {  
            98.             try {  
            99.                 conn.rollback();  
            100.             } catch (SQLException e1) {  
            101.                 e1.printStackTrace();  
            102.             }  
            103.             e.printStackTrace();  
            104.         } finally {  
            105.             try {  
            106.                 conn.close();  
            107.             } catch (SQLException e) {  
            108.                 e.printStackTrace();  
            109.             }  
            110.         }  
            111.         return id;  
            112.   
            113.     }  
            114.   
            115.     /** 
            116.      * 調用存儲過程保存圖片 
            117.      * @param data 
            118.      * @param title 
            119.      * @return 
            120.      */  
            121.     @SuppressWarnings("deprecation")  
            122.     public static Integer saveImageUseProc(byte[] data, String title){  
            123.         Integer id = null;  
            124.         BLOB blob = null;  
            125.         OutputStream outStream;  
            126.         Connection conn = getConnection();  
            127.         try{  
            128.             conn.setAutoCommit(false);  
            129.             String call="{call OPERATE_BLOB.SAVE_BLOB_IMAGE(?,?,?)}";//調用語句  
            130.             CallableStatement proc=conn.prepareCall(call);//調用存儲過程  
            131.             proc.setString(1, title);  
            132.             proc.registerOutParameter(2, Types.BLOB);  
            133.             proc.registerOutParameter(3, Types.INTEGER);   
            134.               
            135.             proc.execute();  
            136.               
            137.             blob = (BLOB)proc.getBlob(2);  
            138.             id = proc.getInt(3);//返回結果  
            139.   
            140.             outStream = blob.getBinaryOutputStream();  
            141.             outStream.write(data, 0, data.length);  
            142.             outStream.flush();  
            143.             outStream.close();  
            144.               
            145.             proc.close();  
            146.             conn.commit();  
            147.         }catch(Exception e){  
            148.             e.printStackTrace();  
            149.         }finally{  
            150.             try {  
            151.                 conn.close();  
            152.             } catch (SQLException e) {  
            153.                 e.printStackTrace();  
            154.             }  
            155.         }  
            156.           
            157.         return id;  
            158.     }  
            159.   
            160.     /** 
            161.      * 輸出保存的圖片 
            162.      * @param response 
            163.      * @param id 
            164.      */  
            165.     public static void outputImage(HttpServletResponse response, Integer id) {  
            166.         Connection con = getConnection();  
            167.         byte[] data = null;  
            168.         try{  
            169.             Statement st = con.createStatement();  
            170.             ResultSet rs = st.executeQuery("select image from t_image where id="  
            171.                     + id);  
            172.             if (rs.next()) {  
            173.                 BLOB blob = (BLOB)rs.getBlob("image");  
            174.                 InputStream inStream = blob.getBinaryStream();  
            175.                 int bufferSize = blob.getBufferSize();  
            176.                 data = new byte[bufferSize];  
            177.                 int count = inStream.read(data, 0, bufferSize);  
            178.                 while(count != -1){//讀出字節數據  
            179.                     response.getOutputStream().write(data,0,count);  
            180.                     count = inStream.read(data, 0, bufferSize);  
            181.                 }  
            182.                 inStream.close();  
            183.             }  
            184.         }catch(Exception e){  
            185.             e.printStackTrace();  
            186.         }finally{  
            187.             try {  
            188.                 con.close();  
            189.             } catch (SQLException e) {  
            190.                 e.printStackTrace();  
            191.             }  
            192.   
            193.         }  
            194.     }  
            195.   
            196.     /** 
            197.      * 調用存儲過程輸出圖片 
            198.      * @param response 
            199.      * @param id 
            200.      */  
            201.     public static void outputImageUseProc(HttpServletResponse response, Integer id){  
            202.         Connection conn = getConnection();  
            203.         try{  
            204.             String call = "{call OPERATE_BLOB.QUERY_BLOB_IMAGE(?,?)}";  
            205.             CallableStatement proc=conn.prepareCall(call);//調用存儲過程  
            206.               
            207.             proc.setInt(1, id);  
            208.             proc.registerOutParameter(2, Types.BLOB);  
            209.               
            210.             proc.execute();  
            211.               
            212.             BLOB blob = (BLOB)proc.getBlob(2);  
            213.               
            214.             InputStream inStream = blob.getBinaryStream();  
            215.             int bufferSize = blob.getBufferSize();  
            216.             byte[] data = new byte[bufferSize];  
            217.             int count = inStream.read(data, 0, bufferSize);  
            218.             while(count != -1){//讀出字節數據  
            219.                 response.getOutputStream().write(data,0,count);  
            220.                 count = inStream.read(data, 0, bufferSize);  
            221.             }  
            222.             inStream.close();  
            223.               
            224.         }catch(Exception e){  
            225.             e.printStackTrace();  
            226.         }finally{  
            227.             try {  
            228.                 conn.close();  
            229.             } catch (SQLException e) {  
            230.                 e.printStackTrace();  
            231.             }  
            232.         }  
            233.     }  
            234.       
            235.     /** 
            236.      * 取得所有表單數據 
            237.      * @param request 
            238.      * @return 
            239.      */  
            240.     @SuppressWarnings("unchecked")  
            241.     public static Iterator<FileItem> getFileItem(HttpServletRequest request) {  
            242.         DiskFileItemFactory factory = new DiskFileItemFactory();  
            243.         factory.setSizeThreshold(4096); // 設置緩沖區大小,這里是4kb  
            244.         ServletFileUpload upload = new ServletFileUpload(factory);  
            245.         upload.setSizeMax(4194304); // 設置最大文件尺寸,這里是4MB  
            246.   
            247.         List<FileItem> items = null;  
            248.         Iterator<FileItem> i = null;  
            249.         try {  
            250.             items = upload.parseRequest(request);  
            251.             i = items.iterator();  
            252.         } catch (FileUploadException e) {  
            253.             e.printStackTrace();  
            254.         }  
            255.   
            256.         return i;  
            257.     }  
            258.   
            259.     /** 
            260.      * 取得數據庫連接 
            261.      *  
            262.      * @return 
            263.      */  
            264.     public static Connection getConnection() {  
            265.         String driver = "oracle.jdbc.driver.OracleDriver";  
            266.         String url = "jdbc:oracle:thin:@195.2.199.6:1521:orcl";  
            267.         Connection conn = null;  
            268.         try {  
            269.             Class.forName(driver);  
            270.             conn = DriverManager.getConnection(url, "testdb", "logcd");  
            271.         } catch (ClassNotFoundException e) {  
            272.             e.printStackTrace();  
            273.         } catch (SQLException ex) {  
            274.             ex.printStackTrace();  
            275.         }  
            276.         return conn;  
            277.     }  
            278.   
            279. }  

            2.所用到的存儲過程
            Sql代碼  收藏代碼
            1. CREATE OR REPLACE PACKAGE BODY OPERATE_BLOB AS  
            2.   
            3.   PROCEDURE  SAVE_BLOB_IMAGE(  
            4.       PC_TITLE  IN   VARCHAR2,  
            5.       PB_IMAGE  OUT   BLOB,  
            6.       PN_ID     OUT  INTEGER  
            7.     )AS  
            8.       v_id INTEGER;  
            9.   BEGIN     
            10.        SELECT nvl(MAX(id),1000) + 1 INTO v_id FROM t_image;   
            11.        PN_ID := v_id;  
            12.        INSERT INTO t_image(id,title,image) values(v_id,PC_TITLE,empty_blob())  
            13.        RETURNING image INTO PB_IMAGE;  
            14.   
            15.   END;  
            16.   
            17.   PROCEDURE QUERY_BLOB_IMAGE(  
            18.      PN_ID      IN   INTEGER,  
            19.      PB_IMAGE   OUT  BLOB  
            20.   )AS  
            21.   BEGIN  
            22.      SELECT image INTO PB_IMAGE FROM t_image WHERE id = PN_ID;    
            23.   END;  
            24.   
            25. END;  


            3.web.xml配置servlet
            Xml代碼  收藏代碼
            1. <servlet>  
            2.     <servlet-name>ImageServlet</servlet-name>  
            3.     <servlet-class>com.logcd.servlet.ImageServlet</servlet-class>  
            4. </servlet>  
            5. <servlet-mapping>  
            6.     <servlet-name>ImageServlet</servlet-name>  
            7.     <url-pattern>/imageServlet</url-pattern>  
            8. </servlet-mapping>  

            4.在image.html頁面中調用下
            Html代碼  收藏代碼
            1. <HTML>  
            2.     <HEAD>  
            3.         <TITLE>Image File</TITLE>  
            4.         <meta http-equiv="Content-Type" content="text/html; charset=gb2312">  
            5.     </HEAD>  
            6.     <FORM method="POST" encType="multipart/form-data" action="imageServlet">  
            7.         <INPUT type="text" name="title">  
            8.         <BR>  
            9.         <INPUT type="file" name="image">  
            10.         <BR>  
            11.         <INPUT type="submit" value="提交">  
            12.     </FORM>  
            13.     <BODY>  
            14.     </BODY>  
            15. </HTML> 

            posted on 2012-06-19 17:42 大龍 閱讀(655) 評論(0)  編輯 收藏 引用

            久久久久亚洲av无码专区喷水| 久久亚洲中文字幕精品一区| 久久久久亚洲AV成人网人人软件| 99久久99久久久精品齐齐| 久久人人爽人人爽人人AV东京热| 99精品国产免费久久久久久下载| 亚洲色欲久久久久综合网| 久久成人小视频| 亚洲AV无码久久| 72种姿势欧美久久久久大黄蕉| 久久91综合国产91久久精品| 免费观看久久精彩视频| 99久久免费只有精品国产| 国内精品久久久久久久亚洲| 精品多毛少妇人妻AV免费久久| 久久综合给合综合久久| 亚洲精品国产第一综合99久久| 亚洲色欲久久久综合网| 9久久9久久精品| 久久午夜无码鲁丝片午夜精品| 欧美伊人久久大香线蕉综合| 久久久久久亚洲AV无码专区| 日韩亚洲欧美久久久www综合网| 久久国产热这里只有精品| 狠狠色丁香婷婷久久综合| 久久精品蜜芽亚洲国产AV| 亚洲精品高清久久| 国产精品99久久久精品无码| 99精品久久精品一区二区| 久久亚洲国产精品123区| 国产亚洲色婷婷久久99精品| 国产福利电影一区二区三区久久老子无码午夜伦不 | 少妇人妻综合久久中文字幕| 人妻无码中文久久久久专区| 草草久久久无码国产专区| 影音先锋女人AV鲁色资源网久久| 久久精品国产亚洲欧美| 日韩人妻无码一区二区三区久久99| 丰满少妇人妻久久久久久| 少妇无套内谢久久久久| 久久性精品|