给定一个指向Web服务器上托管的文件的URL,是否可以将该URL的内容读入clob?如果是这样,怎么样?
答案 0 :(得分:6)
这是一个获取URL并将其内容加载到表中的过程。
使用UTL_HTTP.GET_PIECES()
检索网络文件。这将返回一个字符串数组。如果您在防火墙后面工作,则需要声明您的代理。 Find out more about UTL_HTTP
CLOB争用使用了各种DBMS_LOB功能。该过程声明了一个临时的lob变量,将UTL_HTTP数组的各个部分附加到它,然后最终将其插入表中。 Find out more about DBMS_LOB.
SQL> create or replace procedure pop_file_from_url
2 (p_url in varchar2)
3 is
4 tc clob;
5 lv_web_page utl_http.html_pieces;
6 begin
7
8 dbms_lob.createtemporary(tc, true);
9 dbms_lob.open(tc, dbms_lob.lob_readwrite);
10
11 lv_web_page := utl_http.request_pieces (p_url);
12
13 for i in 1..lv_web_page.count()
14 loop
15 dbms_lob.writeappend(tc, length(lv_web_page(i)) ,lv_web_page(i));
16 end loop;
17
18 insert into t23 values (1, tc);
19
20 dbms_lob.close(tc);
21 dbms_lob.freetemporary(tc);
22
23 end pop_file_from_url;
24 /
Procedure created.
SQL>
如果像我一样,你在11g上,则需要将URL添加到访问控制列表,否则请求将被阻止。 Find out more about ACLs
SQL> exec pop_file_from_url('stackoverflow.com')
BEGIN pop_file_from_url('stackoverflow.com'); END;
*
ERROR at line 1:
ORA-29273: HTTP request failed
ORA-06512: at "SYS.UTL_HTTP", line 1674
ORA-24247: network access denied by access control list (ACL)
ORA-06512: at "APC.POP_FILE_FROM_URL", line 11
ORA-06512: at line 1
SQL>
将StackOverflow的URL添加到我的ACL后,我现在可以将文件插入到我的表中:
SQL> exec pop_file_from_url('stackoverflow.com')
PL/SQL procedure successfully completed.
SQL> select id, dbms_lob.getlength(txt)
2 from t23
3 /
ID DBMS_LOB.GETLENGTH(TXT)
---------- -----------------------
1 208226
SQL>