drop
table
IMAGE_LOB;
CREATE
TABLE
IMAGE_LOB (
T_ID VARCHAR2 (5)
NOT
NULL
,
T_IMAGE BLOB
NOT
NULL
);
CREATE
OR
REPLACE
DIRECTORY IMAGES
AS
'/home/oracle/'
;
CREATE
OR
REPLACE
NONEDITIONABLE
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;
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));
DBMS_LOB.FILECLOSE(F_LOB);
COMMIT
;
END
;
/
BEGIN
IMG_INSERT(
'1'
,
'1.jpg'
);
IMG_INSERT(
'2'
,
'2.jpg'
);
IMG_INSERT(
'3'
,
'1.pdf'
);
END
;
/
select
length(t_image)
from
image_lob;
SELECT
*
FROM
image_lob;
DECLARE
l_file utl_file.file_type;
l_offset
INT
:= 1;
l_amount
INT
:= 32767;
l_len
INT
;
l_buffer RAW(32767);
BEGIN
FOR
cur
IN
(
SELECT
t_image,t.t_id
FROM
image_lob t) LOOP
l_file := utl_file.fopen(
'IMAGES'
, cur.t_id||
'.jpg'
,
'wb'
, 32767);
l_len := dbms_lob.getlength(cur.t_image);
l_offset := 1;
WHILE l_offset < l_len LOOP
dbms_lob.
read
(cur.t_image, l_amount, l_offset, l_buffer);
utl_file.put_raw(l_file, l_buffer,
TRUE
);
l_offset := l_offset + l_amount;
END
LOOP;
utl_file.fclose(l_file);
END
LOOP;
END
;
/