新聞中心
oracle存圖片是我們經(jīng)常需要實(shí)現(xiàn)的功能,下面就教您一個(gè)使用存儲(chǔ)過程實(shí)現(xiàn)oracle存圖片的方法,如果您在oracle存圖片方面遇到過問題,不妨一看。

創(chuàng)新互聯(lián)建站是專業(yè)的寧晉網(wǎng)站建設(shè)公司,寧晉接單;提供成都網(wǎng)站建設(shè)、網(wǎng)站制作,網(wǎng)頁設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行寧晉網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來合作!
要在oracle存圖片 用blob類型,首先在數(shù)據(jù)庫里建立:
--連接到管理員
- 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ù)庫后 創(chuàng)建一個(gè)BlobDAO類 用來取出表中的blob類型圖片
- public class BlobDAO {
- private static final BlobDAO instance = new BlobDAO();
- private Connection conn = null;
- private BlobDAO() {
- }
- public static BlobDAO getInstance() {
- return instance;
- }
- private void initConn() {
- conn = DBAccess.getInstance().getConn();
- }
- public byte[] getImage(String imgname) {
- BufferedInputStream ins;//取得BLOB的IO流
- byte[] bt = null;
- initConn();
- Blob bo = null;
- PreparedStatement ps = null;
- ResultSet rs = null;
- String sql = "select T_IMAGE from IMAGE_LOB where t_id=?";
- try {
- ps = conn.prepareStatement(sql);
- ps.setString(1, imgname);
- rs = ps.executeQuery();
- if (rs.next()) {
- bo = rs.getBlob("T_IMAGE");
- try {
- ins = new BufferedInputStream(bo.getBinaryStream());
- int bufferSize = (int) bo.length();//取得BLOB的長度
- bt = new byte[bufferSize];
- try {
- ins.read(bt, 0, bufferSize);
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- //建立字節(jié)緩存
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } finally {
- try {
- rs.close();
- ps.close();
- conn.close();
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- return bt;
- }
- }
在action里面調(diào)用getImage()方法并顯示圖片在頁面上
- public ActionForward execute(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response) {
- // TODO Auto-generated method stub
- BlobDAO blobDAO = BlobDAO.getInstance();
- byte[] bs = blobDAO.getImage("1");
- try {
- response.getOutputStream().write(bs);
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return null;
- }
添加圖片到數(shù)據(jù)庫
請(qǐng)?jiān)赾盤下放入圖片--c:\\4.gif
- public void savaImg(String imgId) {
- //傳的是存入數(shù)據(jù)庫圖片的id
- initConn();
- Statement st = null;
- BLOB blob = null; //圖片類型
- OutputStream outputStream = null; //輸出流
- File file = null; //文件
- InputStream inputStream = null; //輸入流
- ResultSet rs = null;
- try {
- conn.setAutoCommit(false); //事物由程序員操作
- st = conn.createStatement();
- st.executeQuery("insert into IMAGE_LOB values('"+ imgId +"',empty_blob())");
- rs = st.executeQuery("select T_IMAGE from IMAGE_LOB where t_id='"+ imgId +"' for update");
- if (rs.next()) {
- blob = (BLOB) rs.getBlob(1);
- outputStream = blob.getBinaryOutputStream();
- file = new File("c:\\4.gif");
- inputStream = new FileInputStream(file);
- byte[] b = new byte[blob.getBufferSize()];
- int len = 0;
- while ((len = inputStream.read(b)) != -1) {
- System.out.println(len);
- outputStream.write(b, 0, len);
- }
- }
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } finally {
- try {
- inputStream.close();
- outputStream.flush();
- outputStream.close();
- rs.close();
- st.close();
- conn.commit();
- conn.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
【編輯推薦】
oracle樹查詢的實(shí)現(xiàn)
oracle查詢當(dāng)前時(shí)間的實(shí)現(xiàn)
帶您了解Oracle層次查詢
帶您深入了解Oracle臨時(shí)表
Oracle with語句的用法
當(dāng)前文章:使用存儲(chǔ)過程實(shí)現(xiàn)oracle存圖片的方法
標(biāo)題鏈接:http://m.fisionsoft.com.cn/article/djseici.html


咨詢
建站咨詢
