from web and saving it into the disk using PL/SQL. I am re-posting
it into my BLOG for future reference.
SQL> CREATE OR REPLACE DIRECTORY TEST_DIR as 'C:\';
Directory created.
SQL> /* Example of downloading a pdf from web and saving into disk */
SQL> DECLARE
2 lv_url VARCHAR2(100) := 'http://www.oracle.com/technetwork/database/features/plsql/overview/plsql-new-in-11gr1-128133.pdf';
3 lc_return BLOB;
4 lhttp_url httpuritype;
5 ---Varriables declared for writing the LOB to pdf file --
6 l_file UTL_FILE.FILE_TYPE;
7 l_buffer RAW(32767);
8 l_amount BINARY_INTEGER := 32767;
9 l_pos INTEGER := 1;
10 l_blob BLOB;
11 l_blob_len INTEGER;
12 BEGIN
13 --create uri
14 lhttp_url := httpuritype.createuri(lv_url);
15 --get the PDF document
16 lc_return := lhttp_url.getblob();
17 -- Open the destination file.
18 l_file := UTL_FILE.FOPEN('TEST_DIR', 'mypdf.pdf', 'wb');
19 --Get the total length of the BLOB
20 l_blob_len := DBMS_LOB.getlength(lc_return);
21 -- Read chunks of the BLOB and write them to the file
22 -- until complete.
23 WHILE l_pos < l_blob_len LOOP
24 DBMS_LOB.READ(lc_return, l_amount, l_pos, l_buffer);
25 UTL_FILE.put_raw(l_file, l_buffer, FALSE);
26 l_pos := l_pos + l_amount;
27 END LOOP;
28 -- Close the file.
29 UTL_FILE.FCLOSE(l_file);
30 EXCEPTION
31 WHEN OTHERS THEN
32 -- Close the file if something goes wrong.
33 IF UTL_FILE.IS_OPEN(l_file) THEN
34 UTL_FILE.FCLOSE(l_file);
35 END IF;
36
37 RAISE;
38 END;
39 /
PL/SQL procedure successfully completed.
SQL>
And the file saved successfully.
C:\>dir *.pdf
Volume in drive C has no label.
Volume Serial Number is 6806-ABBD
Directory of C:\
12/22/2010 02:56 PM 170,264 mypdf.pdf
1 File(s) 170,264 bytes
0 Dir(s) 4,829,433,856 bytes free
C:\>
Works perfectly, many thanks for sharing :)
ReplyDelete