Chinaunix首页 | 论坛 | 博客
  • 博客访问: 103783083
  • 博文数量: 19283
  • 博客积分: 9968
  • 博客等级: 上将
  • 技术积分: 196062
  • 用 户 组: 普通用户
  • 注册时间: 2007-02-07 14:28
文章分类

全部博文(19283)

文章存档

2011年(1)

2009年(125)

2008年(19094)

2007年(63)

分类: Oracle

2008-04-21 20:29:40

  二、Oracle 9iAS的标签库和Bean提供的文件上载功能

  Oracle developer suit 9i 中的Jdeveloper9031提供通过标签库上载文件的方法,下面的例子中in_file.jsp文件提供上载表单,up_file.jsp列出上载文件列表,dn_file.jsp文件为下载刚才上载的文件。该方法使用图形编辑器,简单可行,但不支持中文文件名,可实现客户端文件上载和下载。

  如下为in_file.jsp的源程序:

<%@ taglib uri=""
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="" prefix="database" %>
<%@ taglib uri="" 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="" prefix="fileaccess" %>
<%@ taglib uri="" 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>

阅读(449) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~