Oracle中有多种方法可以向数据库或服务器文件系统上载文件,这里主要介绍如下三种:Oracle HTTP Server(OHS)的mod_plsql模块提供通过web页面向数据库上载文件的功能;Oracle
9iAS的标签库和Bean提供的文件上载功能;通过JDBC程序上载文件。
一、Oracle HTTP Server(OHS)的mod_plsql模块提供通过web页面向数据库上载文件的功能
1.在文件上载的目标模式中建立文档访问表
CREATE TABLE [table_name] (
NAME VARCHAR2(256) UNIQUE NOT NULL,
MIME_TYPE VARCHAR2(128),
DOC_SIZE NUMBER,
DAD_CHARSET VARCHAR2(128),
LAST_UPDATED DATE,
CONTENT_TYPE VARCHAR2(128),
[content_column_name] [content_column_type]
[ , [content_column_name] [content_column_type]]
);
表中CONTENT_TYPE列为BLOB或long raw,但只能有一列。每个上载文件在表中单独占有一行,不能同时把多个文件上载到表中的同一行内,这是mod_plsql模块功能有限的地方。用下面和列建立文档表:Content_table。
NAME VARCHAR(128) UNIQUE NOT NULL,
MIME_TYPE VARCHAR(128),
DOC_SIZE NUMBER,
DAD_CHARSET VARCHAR(128),
LAST_UPDATED DATE,
CONTENT_TYPE VARCHAR(128),
CONTENT LONG RAW,
BLOB_CONTENT BLOB ;
2.配置DAD的文档访问表
启动数据库和OHS后,创建空白的数据库访问描述符。在"数据库访问描述符"页面的"网关使用文档表"信息向与此数据库访问描述符关联的数据库上载/下载文档。缺省情况下所有文档都将以
BLOB 上载。通过在 Long Raw 字段中指定以逗号分隔的文件扩展名,可以覆盖此缺省设置。此字段的*值将使所有文档以 Long
Raw 上载。"文档访问过程"指定用于访问文档的虚拟路径。"文档访问过程"字段指定网关处理文档请求所调用的过程。这里指定缺省的文档表为content_table。
3.提供文件上载页面,这里使用存储过程的形式上载和浏览、下载文件。
CREATE OR REPLACE PROCEDURE "ZYFILEUP"."IN_FILE"
is
begin
htp.prn('
<html>
<head>
<title>File upload</title>
</head>
<body bgcolor="#C0C0C0">
<form enctype="multipart/form-data" action="/pls/upfile/write_infos"
method="POST">
<p><b><font size="5" color="#800080">指定上载文件</font></b></p>
<hr>
<p>
<b>标题:<textarea rows="3" name="filetitle" cols="100"
style="background-color: #008080; color: #FFFFFF"></textarea>
<br>
<b>类别:<input type="text" name="filesort" size="130"
style="background-color: #008080; color: #FFFFFF"> <br>
<b>作者:<input type="text" name="author" size="130"
style="background-color: #008080; color: #FFFFFF"> <br>
<b>小类:<input type="text" name="localsort" size="130"
style="background-color: #008080; color: #FFFFFF"> <br>
<b>摘要:<textarea rows="5" name="abstract" cols="130"
style="background-color: #008080; color: #FFFFFF"></textarea>
<br>
<b>文章1:<input type="file" name="up_file" size="110"
style="background-color: #008080; color: #FFFFFF"> <br>
<b>文章2:<input type="file" name="up_file" size="110"
style="background-color: #008080; color: #FFFFFF"> <br>
<b>文章3:<input type="file" name="up_file" size="110"
style="background-color: #008080; color: #FFFFFF"> <br>
<b>文章4:<input type="file" name="up_file" size="110"
style="background-color: #008080; color: #FFFFFF"> <br>
</b></b></b></b></b></b></b></b></b></b> </p><hr><p><b>
<line>
<input type="submit" value="提交">
<input type="reset" value="全部重写">
</p>
</form>
</body>
</html>
');
end;
4.提供上载文件浏览页面
第3步提交的文件自动上载到文档表中,下面由存储过程WRITE_INFOS来记录上载的文件信息,实际上完全没必要,但这里做一比较。
CREATE OR REPLACE PROCEDURE "ZYFILEUP"."WRITE_INFOS"
(
filetitle in varchar2,
up_file in owa.vc_arr,
filesort in varchar2,
author in varchar2,
abstract in varchar2,
localsort in varchar2
) is
i number:=1;
begin
htp.htmlopen;
htp.headopen;
htp.title('File Uploaded');
htp.headclose;
htp.bodyopen;
htp.header(1, 'Upload Status');
while true loop
begin
htp.prn('Uploaded '|| up_file(i) ||' successfully !');
htp.prn('<br>');
htp.prn('<a href="/pls/upfile/downfile?upfilename=');
htp.prn(utl_url.escape(up_file(i),true,'GBK'));
htp.prn('" target="_blank">Open this file !</a>');
htp.prn('<br>');
INSERT INTO "ZYFILEUP"."FILE_TABLE"
values(filetitle,up_file(i),filesort,author,sysdate(),abstract,localsort);
commit;
i:=i+1;
exception
when no_data_found then exit;
end;
end loop;
htp.line;
htp.print('Uploaded successfully !');
htp.bodyclose;
htp.htmlclose;
end;
查阅文档表后会发现,文件已经上载了,但文档表的上载时间有错误,与上载记录中的系统时间不符,这可能是该功能的一个BUG,如果谁有解决此问题的办法或软件补丁,请告诉我。
5.为上载文件提供页面浏览功能
CREATE OR REPLACE PROCEDURE "ZYFILEUP"."DOWNFILE"
(upfilename
in varchar2)
is
mimetype varchar2(100);
myblob blob;
begin
select mime_type,blob_content into mimetype,myblob
from zyfileup.content_table where name = upfilename;
owa_util.mime_header(mimetype, true);
sys.wpg_docload.download_file(myblob);
end;
这样可以通过Web浏览器根据上载文件的mime_type类型浏览文件了。
这种上载文件的办法最为简单,可以支持中文文件名,支持客户端文件上载和下载,可以自动识别文件类型和计算文件大小。
二、Oracle 9iAS的标签库和Bean提供的文件上载功能
Oracle developer suit 9i 中的Jdeveloper9031提供通过标签库上载文件的方法,下面的例子中in_file.jsp文件提供上载表单,up_file.jsp列出上载文件列表,dn_file.jsp文件为下载刚才上载的文件。该方法使用图形编辑器,简单可行,但不支持中文文件名,可实现客户端文件上载和下载。
如下为in_file.jsp的源程序:
<%@ taglib uri="http://xmlns.oracle.com/j2ee/jsp/tld/ojsp/fileaccess.tld"
prefix="fileaccess" %>
<%@ page language="java" import="java.io.*"
contentType="text/html" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/HTML;charset=gb2312">
<title>jdbc upload and download blob</title>
</head>
<body>
<fileaccess:httpUploadForm
formsAction="up_file.jsp"
maxFiles="5" fileNameSize="100"
maxFileNameSize="150" submitButtonText="send">
</fileaccess:httpUploadForm>
</body> </html>
up_file.jsp的源程序:
<%@ taglib uri="http://xmlns.oracle.com/j2ee/jsp/tld/ojsp/sqltaglib.tld"
prefix="database" %>
<%@ taglib uri="http://xmlns.oracle.com/j2ee/jsp/tld/ojsp/fileaccess.tld"
prefix="fileaccess" %>
<%@ page language="java" contentType="text/html;
charset=gb2312"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/HTML;charset=gb2312">
<title>jdbc upload and download blob</title>
</head>
<body>
<database:dbOpen user="zy" password="zy"
URL="jdbc:oracle:thin:@db92:1521:cf92" commitOnClose="true">
<fileaccess:httpUpload
destination="zy_blob"
destinationType="database"
table="blob_table">
</fileaccess:httpUpload>
</database:dbOpen>
Done!
<a href="dn_file.jsp">下载!</a>
</body></html>
dn_file.jsp的源程序:
<%@ taglib uri="http://xmlns.oracle.com/j2ee/jsp/tld/ojsp/fileaccess.tld"
prefix="fileaccess" %>
<%@ taglib uri="http://xmlns.oracle.com/j2ee/jsp/tld/ojsp/sqltaglib.tld"
prefix="database" %>
<%@ page contentType="text/html;charset=GBK"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=GBK">
</head>
<body>
<database:dbOpen user="zy" password="zy"
URL="jdbc:oracle:thin:@db92:1521:cf92">
<fileaccess:httpDownload source="*" servletPath="/"
sourceType="database" table="blob_table">
</fileaccess:httpDownload>
</database:dbOpen>
Download done!
</body>
</html>
三、通过JDBC实现文件上载和下载
通过把文件上载到BLOB或CLOB列实现文件上载,但此法不支持客户端文件上载,所以局限性很大,通过JDBC实现的下载功能也只是在服务器的本地下载(下载程序没有列出),所以这种方法仅作为一项技术参考可以,没有太大实用价值。
Blob_in_stream.jsp程序如下:
<%@ page session="true" language="java" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/HTML;charset=gb2312">
<title>jdbc upload and download blob</title>
</head>
<body>
<FORM action="blob_w_stream.jsp" method="POST">
<p>文件 File to upload:<INPUT type="file" name="up_file"><br>
<p><INPUT type="submit">
</FORM>
</body>
</html>
Blob_w_stream.jsp程序如下:
<%@ page session="true" language="java" contentType="text/html;
charset=gb2312"%>
<%@ page import="java.sql.*"%>
<%@ page import="java.io.*"%>
<%@ page import="oracle.jdbc.driver.*" %>
<%@ page import="oracle.sql.*" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/HTML;charset=gb2312">
<title>jdbc upload and download blob</title>
</head>
<body>
<%
String uploadfile=request.getParameter("up_file");
//Input form don't have ENCTYPE="multipart/form-data"
clause,else input
//file name will NULL!Get file name from input form.
File binaryFile = new File(uploadfile);
Reader char_stream = clobtext.getCharacterStream();
char [] char_array = new char [length];
int chars_read = char_stream.read (char_array, 0, length);
out.println(char_array);
out.println();
out.write();
Inputstream asciiChar_stream = clobtext.getAsciiStream();
byte[] asciiChar_array = new byte[length];
int asciiChar_read = asciiChar_stream.read(asciiChar_array,0,length);
//Or File binaryFile = new File("c:\\cf\\a.doc"); point
to c:\cf\a.doc.
FileInputStream instream = new FileInputStream(binaryFile);
%>
<p>file name is :<%=uploadfile%>
<%
Class.forName("oracle.jdbc.OracleDriver").newInstance();
String connStr="jdbc:oracle:thin:@db92:1521:cf92";
String user="zy";
String password="zy";
Connection conn = DriverManager.getConnection(connStr,user,password);
conn.setAutoCommit(false);
Statement stmt=conn.createStatement();
stmt.execute("UPDATE ZY.BLOB_TABLE SET DATA = EMPTY_BLOB()
WHERE FILENAME='Concepts.pdf'");
conn.commit();
//must null the blob data column,else if size of inserting file
less than
//existing blob file,then new insert file format error occur!New
blob file don't
//overwrite old blob column data!
String cmd = "SELECT DATA FROM ZY.BLOB_TABLE WHERE FILENAME='Concepts.pdf'
for update";
ResultSet rset = stmt.executeQuery(cmd);
BLOB blob;
rset.next();//must.else occur error "don't call ResultSet.next()"
blob = ((OracleResultSet)rset).getBLOB(1);
//must explicit!dont locate in logical block.for example,locate
in IF...ITEN.
OutputStream outstream = blob.getBinaryOutputStream();
int size = blob.getBufferSize();
%>
<p>buffer size is :<%=size%>
<%
byte[] buffer = new byte[102400];
//Or direct 102400 instead of variable size!
//Performance tips!size=24396.
int length = -1;
while ((length = instream.read(buffer)) != -1)
outstream.write(buffer, 0, length);
instream.close();
outstream.close();
conn.commit();//must explicit commit!If BFILE,then auto commit.
conn.close();
%>
<p>Upload file is down!
</body>
</html>
|