使用存储过程实现oracle存图片的方法

数据库 Oracle
oracle数据库中存储图片,方法很多,下文介绍了一个使用存储过程在oracle数据库中存储图片的方法,希望对您能有所启迪。

oracle存图片是我们经常需要实现的功能,下面就教您一个使用存储过程实现oracle存图片的方法,如果您在oracle存图片方面遇到过问题,不妨一看。

要在oracle存图片 用blob类型,首先在数据库里建立:

--连接到管理员

  1. conn sys/tbsoft as sysdba; 

--为scott用户授权

  1. grant create any directory to scott; 

--回到scott用户

  1. conn scott/tiger; 

--创建存储图片的表

  1. CREATE TABLE IMAGE_LOB (T_ID VARCHAR2 (5) NOT NULL,T_IMAGE BLOB NOT NULL); 

--创建存储图片的目录

  1. CREATE OR REPLACE DIRECTORY IMAGES AS 'C:\picture'; 

--在c:下自己建一个叫picture的文件夹

  1. CREATE OR REPLACE PROCEDURE IMG_INSERT (TID VARCHAR2,FILENAME VARCHAR2) AS 

F_LOB BFILE;--文件类型

B_LOB BLOB;

 

  1. BEGIN     
  2. iNSERT INTO IMAGE_LOB (T_ID, T_IMAGE)     
  3. VALUES (TID,EMPTY_BLOB ()) RETURN T_IMAGE INTO B_LOB    

 

--插入空的blob

  1. F_LOB:BFILENAME ('IMAGES', FILENAME); 

--获取指定目录下的文件

  1. DBMS_LOB.FILEOPEN(F_LOB, DBMS_LOB.FILE_READONLY); 

--以只读的方式打开文件

  1. DBMS_LOB.LOADFROMFILE (B_LOB, F_LOB,DBMS_LOB.GETLENGTH (F_LOB)); 

--传递对象

  1. DBMS_LOB.FILECLOSE (F_LOB); 

--关闭原始文件

  1. COMMIT;  
  2. END;  
  3. /  

--在C:\picture下放一张图片1.gif

--将该图片存入表

  1. call IMG_INSERT('1','1.gif'); 

然后创建一个web项目 连接数据库后 创建一个BlobDAO类 用来取出表中的blob类型图片

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

在action里面调用getImage()方法并显示图片在页面上

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

添加图片到数据库

请在c盘下放入图片--c:\\4.gif

  1. public void savaImg(String imgId) {  
  2.            //传的是存入数据库图片的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.            } catch (SQLException e) {  
  28.                  // TODO Auto-generated catch block  
  29.                  e.printStackTrace();  
  30.            } catch (FileNotFoundException e) {  
  31.                  // TODO Auto-generated catch block  
  32.                  e.printStackTrace();  
  33.            } catch (IOException e) {  
  34.                  // TODO Auto-generated catch block  
  35.                  e.printStackTrace();  
  36.            } finally {  
  37.                  try {  
  38.                        inputStream.close();  
  39.                        outputStream.flush();  
  40.                        outputStream.close();  
  41.                        rs.close();  
  42.                        st.close();  
  43.                        conn.commit();  
  44.                        conn.close();  
  45.                  } catch (IOException e) {  
  46.                        // TODO Auto-generated catch block  
  47.                        e.printStackTrace();  
  48.                  } catch (SQLException e) {  
  49.                        // TODO Auto-generated catch block  
  50.                        e.printStackTrace();  
  51.                  }  
  52.            }  
  53.      }  
  54.  
  55.    

 

 

 

【编辑推荐】

oracle树查询的实现

oracle查询当前时间的实现

带您了解Oracle层次查询

带您深入了解Oracle临时表

Oracle with语句的用法

 

责任编辑:段燃 来源: 互联网
相关推荐

2010-10-29 16:12:51

Oracle存储过程

2010-10-29 16:06:55

Oracle存储过程

2010-11-16 14:30:32

Oracle存储过程

2011-05-18 10:07:13

oracle存储

2010-10-29 16:17:55

Oracle存储过程

2010-10-26 14:50:11

oracle存储过程

2017-09-04 11:48:56

MybatisOracle存储过程

2009-05-13 10:29:01

存储过程OracleJava

2010-11-12 12:01:08

Oracle存储过程

2009-09-17 11:32:52

LINQ调用存储过程

2010-04-15 17:08:20

Oracle存储过程

2011-07-08 16:07:41

Oracle job存储过程

2010-04-16 10:11:20

Oracle存储过程

2010-04-21 10:37:02

Oracle创建

2010-04-07 13:12:25

Oracle存储过程

2010-05-05 17:19:32

Oracle存储过程

2010-04-07 12:08:28

Oracle存储过程

2010-04-26 14:12:23

Oracle使用游标触

2010-04-15 17:31:10

Oracle存储过程

2010-04-08 16:41:29

Oracle存储过程
点赞
收藏

51CTO技术栈公众号