利用oracle自带的utl_file包可以访问磁盘文件,但有个限制--
无法访问文件夹?什么意思呢?就是说oracle只能访问指定的文件,而不能访问文件夹下的未知文件。所以,如果要通过oracle去遍历某指定路径下的
所有文件,sorry, impossible! 但是,有一种替代方案可以助你一臂之力!
众所周知java之File类指向的可以是目录也可以是文件,如果指向目录,可以用file.list()找到包含的文件及目录,so
that,我可以用java来遍历文件,然后返回一个值给oracle再进行后续操作。
Come on, let's talk about this case!
oracle调用java可以分三个步骤:
{dy}步:在PLSQL客户端里写java代码(类及方法),实现oracle无法做的工作或你想要让java做的工作;
-
--功能:返回所有文件字段串??
-
create?or?replace?and?compile?java?source?named?file_list?as??
-
import
?java.io.File;??
-
public
?
class
?file_list??
-
{??
-
??public
?
static
?String?entry(String?path)??
-
??{??
-
????StringBuffer?resultBuffer?=?new
?StringBuffer();??
-
????try
{??
-
??????File?file?=?new
?File(
"I:\\"
+path);??
-
??????resultBuffer.append(file.getAbsolutePath());??
-
??????String[]?files?=?file.list();??
-
????????
-
??????resultBuffer.append("----"
);??
-
??????for
(
int
?i=
0
;?i<files.length;?i++){??
-
??????????StringBuffer?fileStr?=?new
?StringBuffer(
"<P?align='center'><IMG?src="
http:
??
-
??????????fileStr.append(path.replace("?mce_src="
http:
??
-
??????????fileStr.append(path.replace("\\',?'
/'));??
-
??????????fileStr.append(files[i]);??
-
??????????fileStr.append("'></P>"
);??????
-
??????????resultBuffer.append(fileStr.toString());??
-
??????}??
-
????}catch
(Exception?ex){??
-
??????resultBuffer.append("----"
);??
-
??????resultBuffer.append(ex);??
-
????}??????
-
????return
?resultBuffer.toString();??????
-
??}??
-
}??
第二步:写oracle函数(function),调用java类方法;
第三步:写oracle存储过程(procedure)或客户端调用程序;
-
create?or?replace?procedure?proce_transfer_data?as??
-
?url?varchar2(100
);??
-
?path?varchar2(100
);??
-
?img_url?varchar2(2000
);??
-
--档案公布带图数据??
-
??cursor?cur1?is??
-
???select?*?from?ARCHIVE_PUBLICATION?t?where?t.big_event_id?is?null
?and?t.appendix_numb?>?
0
?and?t.publish_order?is?not?
null
;??
-
??r1?ARCHIVE_PUBLICATION%rowtype;??
-
begin??
-
??open?cur1;??
-
????loop??
-
??????fetch?cur1?into?r1;????????????
-
??????exit?when?cur1%notfound;??
-
??????--找出重复的数据???
-
?????????
-
??????--图片目录??
-
??????if
?r1.publish_order?is?
null
?then?????????
-
????????path?:=??'archives_files\'?||?substr(r1.archive_no,?1,?5)?||?'
\
'??||?substr(r1.archive_no,?6,?3)??||?'
\'?||???
-
????????substr(r1.archive_no,?9
,?
5
)||?
'\'??||??substr(r1.archive_no,?14,?4)?||?'
\';??
-
??????else
???
-
????????path?:=?'archives_files\ziliao\'?||?r1.publish_order?||?'
\';????????
-
??????end?if
;??
-
??????--dbms_output.put_line(path);??
-
?????--授权??
-
?????dbms_java.grant_permission(?'QDDA'
,?
'SYS:java.io.FilePermission'
,?
'I:\archives_files\',?'
READ'?);??
-
????????
-
??????--图片路径??
-
??????img_url?:=?func_file_list(path);?????????
-
?????dbms_output.put_line(r1.id?||?img_url);??
-
????????
-
??????update?subject_articles?t?set?t.content?=?img_url?||?r1.content||','
||r1.file_dz||
','
||r1.archive_no||
','
||r1.file_no,???
-
???????t.secondid?=?'12466747755782031001'
,?t.rootid=
'12264731996871389001'
,?t.area?=?
'qd'
??
-
????????where?t.id?=?r1.id;????
-
??????commit??;??????
-
????end?loop;??
-
??????
-
??close?cur1;??
-
??commit;???
-
????
-
end?proce_transfer_data;?