用java实现ORACLE数据库的文件上传、下载

发表于:2007-07-14来源:作者:点击数: 标签:
用java实现ORACLE 数据库 的文件上传、下载 文件上传使用:http://www.javazoom.net 的 UploadBean JAVA包,他的文件上传做的还不错,可进行目录或数据库的文件上传,默认使用的是ORACLE 的LONGRAW子段类型,可使用BLOB类型,但需要使用扩展JAVA包,LONGRAW
用java实现ORACLE数据库的文件上传、下载

文件上传使用:http://www.javazoom.net 的 UploadBean JAVA包,他的文件上传做的还不错,可进行目录或数据库的文件上传,默认使用的是ORACLE 的LONGRAW子段类型,可使用BLOB类型,但需要使用扩展JAVA包,LONGRAW子段最大长度为2GB,并且一个表内只允许出现一个LONGRAW型子段,不过已经够我用了,而BLOB型子段最大为4GB,而且没有表中出现次数的限制。备注:想用他下载的组件Download4J 1.0,我劝还是别做梦了,在功能上不太适合用在数据库下载

文件下载使用的是:http://www.jspsmart.com的SmartUpload JAVA包,该包即可以文件上传,也可以文件下在,而且使用也比较方便,不过我在这里只使用文件从数据库下载功能,有兴趣的朋友可以试试。

下面我把使用这两个包的源CODE给大家说一下,以后大家碰道这些问题就不用象我一样发愁了,哎!多伟大的人呀!大家给点掌声安慰我这个受伤的孤独男人吧!

文件上传:
实际上这些JAVA包都有使用的例子,不过我还是要贴出来。 
文件名:ChineseDatabaseUpload.jsp
文件内容:
<html>
<%@ page language="java" contentType="text/html; charset=gb2312" import="javazoom.upload.*,java.util.*" %>
<%@ page errorPage="ExceptionHandler.jsp" %>
<jsp:useBean id="upBean" scope="application" class="javazoom.upload.UploadBean" >

<%
Properties props = new Properties();
props.put("user","cepic");
props.put("password","lpsc");
upBean.setDatabasestore("oracle.jdbc.driver.OracleDriver","jdbc:oracle:thin:@10.231.234.252:1521:cepic",props); //数据库连接
%>

</jsp:useBean>
<head>
<title>中文上传</title>
<meta http-equiv="Content-Type" content="text/html; charset="gb2312">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<ul><font size="-1" face="Verdana, Arial, Helvetica, sans-serif">
<%
request.setCharacterEncoding("gb2312");
if (MultipartFormDataRequest.isMultipartFormData(request))
{
// Uses MultipartFormDataRequest to parse the HTTP request.
MultipartFormDataRequest mrequest = new MultipartFormDataRequest(request,null,100*1024*1024,MultipartFormDataRequest.COSPARSER,"gb2312");
String todo = mrequest.getParameter("todo");
if ( (todo != null) && (todo.equalsIgnoreCase("upload")) )
{
Hashtable files = mrequest.getFiles();
if ( (files != null) || (!files.isEmpty()) )
{
UploadFile file = (UploadFile) files.get("uploadfile");
out.println("<li>Form field(表格栏): uploadfile"+"<BR> Uploaded file(已上传的文件名称): "+file.getFileName()+" ("+file.getFileSize()+" bytes)"+"<BR> Content Type(文件类别)"+file.getContentType());
//out.println("<li>Form field: uploadfile"+"<BR> Uploaded file: "+file.getFileName()+" ("+file.getFileSize()+" bytes)"+"<BR> Content Type"+file.getContentType());
// Uses the bean now to store specified by jsp:setProperty at the top.
upBean.store(mrequest, "uploadfile");
}
else
{
out.println("<li>No uploaded files");
}
out.println("<BR> gb2312 Text(简体) = "+mrequest.getParameter("gb2312txt"));
}
else out.println("<BR> todo="+todo);
}
%>
</font></ul>
<form method="post" action="ChineseDatabaseUpload.jsp" name="upform" enctype="multipart/form-data">
<table width="60%" border="0" cellspacing="1" cellpadding="1" align="center">
<tr>
<td align="left"><font size="-1" face="Verdana, Arial, Helvetica, sans-serif"><b>请选择上传文件:</b></font></td>
</tr>
<tr>
<td align="left"><font size="-1" face="Verdana, Arial, Helvetica, sans-serif">
<input type="file" name="uploadfile" size="50">
</font></td>
</tr>
<tr>
<td align="left"><font size="-1" face="Verdana, Arial, Helvetica, sans-serif">
gb2312 text:
<input type="text" name="gb2312txt" size="20">
</font></td>
</tr>
<tr>
<td align="left"><font size="-1" face="Verdana, Arial, Helvetica, sans-serif">
<input type="hidden" name="todo" value="upload">
<input type="submit" name="Submit" value="上传">
<input type="reset" name="Reset" value="取消">
</font></td>
</tr>
</table>
<br>
<br>
<table width="90%" border="0" cellspacing="1" cellpadding="0" align="center">
<tr>
<td bgcolor="#666666">
<table width="100%" border="0" cellspacing="1" cellpadding="0" align="center">
<tr>
<td bgcolor="#FFFFFF"><font size="-1" face="Verdana, Arial, Helvetica, sans-serif"><b><font color="#0000FF"> Needed
HTML tags :</font></b></font></td>
</tr>
<tr>
<td bgcolor="#FFFFFF"><font size="-1" face="Verdana, Arial, Helvetica, sans-serif"> <<b>form</b>
<b>method</b>="<b><font color="#FF0000">post</font></b>"
<b>action</b>="<b><font color="#FF0000">ChineseUpload.jsp</font></b>"
name="upload" <b>enctype</b>="<b><font color="#FF0000">multipart/form-data</font></b>"></font></td>
</tr>
<tr>
<td bgcolor="#FFFFFF"><font size="-1" face="Verdana, Arial, Helvetica, sans-serif"> <<b>input</b>
<b>type</b>="<b><font color="#FF0000">file</font></b>"
<b>name</b>="<font color="#FF0000"><b>uploadfile</b></font>"
size="50"></font></td>
</tr>
</table>
</td>
</tr>
</table>
<p> </p>
<p> </p>
<p align="center"> </p>
<p align="center"> </p>
<p align="center"> </p>
<p align="center"> </p>
</form>
</body>
</html>

该ORACLE的表结构,在该网站上找吧!很简单就三个子段而已。

文件下载:
文件名:jspsmartdownload.jsp
文件内容:
<meta http-equiv="Content-Type" content="application/msword; charset=gb2312"> //注意这里,如果你没有设置成“GB2312”结果你会明白的。
<%@ page language="java" import="java.sql.*,com.jspsmart.upload.*"%>
<jsp:useBean id="mySmartUpload" scope="page" class="com.jspsmart.upload.SmartUpload" />
<%
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();

Connection con = DriverManager.getConnection("jdbc:oracle:thin:@10.231.234.252:1521:cepic", "cepic", "lpsc");

Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM UPLOADS");

if (rs.next()){
mySmartUpload.initialize(pageContext);
mySmartUpload.downloadField(rs,"BINARYFILE","application/msword","");
//mySmartUpload.fieldToFile(rs,"BINARYFILE","c:/sample.DOC");
}
rs.close();
stmt.close();
con.close();
%>

原文转自:http://www.ltesting.net