先把文件读取到内存,再以二进制格式保持到数据库中的大字段中(clob或clob)。
写大对象。
Java code
public static void main(String[] args) {
// TODO Auto-generated method stub
Connection conn = null;
Statement stat = null;
ResultSet rs = null;
OutputStream os = null;
FileInputStream fis = null;
int bs = 0;
try {
ClassforName("oraclejdbcdriverOracleDriver");
conn = DriverManagergetConnection("jdbc:oracle:thin:@localhost:1521:oraDB","bigfou","---");
connsetAutoCommit(false);
stat = conncreateStatement();
statexecuteUpdate("insert into t_video(id,video) values(1,empty_blob())");
rs = statexecuteQuery("select video from t_video where id = 1");
rsnext();
oraclesqlBLOB blo = (oraclesqlBLOB)rsgetBlob(1);
os = blogetBinaryOutputStream();
bs = blogetBufferSize();
fis = new FileInputStream("D:\\Temp\\MPlayer-CVS-20040808-K&K\\mplayerexe");
byte[] buf = new byte[bs];
int length = 0;
while(true)
{
length = fisread(buf);
if(length == -1) break;
oswrite(buf,0,length);
}
osclose();
os = null;
fisclose();
fis = null;
conncommit();
connsetAutoCommit(true);
connclose();
} catch(Exception ex) {
exprintStackTrace();
}
}
读大对象
Java code
InputStream is = null;
FileOutputStream fos = null;
byte[] buf = null;
int bs = 0;
try {
ClassforName("oraclejdbcdriverOracleDriver");
conn = DriverManagergetConnection("jdbc:oracle:thin:@localhost:1521:oraDB","bigfou","-");
connsetAutoCommit(false);
stat = conncreateStatement();
rs = statexecuteQuery("select video from t_video where id = 1");
rsnext();
oraclesqlBLOB blo = (oraclesqlBLOB)rsgetBlob(1);
bs = blogetBufferSize();
buf = new byte[bs];
int length = 0;
is = blogetBinaryStream();
fos = new FileOutputStream("d:\\testexe");
while(true) {
length = isread(buf);
if(length == -1) break;
foswrite(buf,0,length);
}
fosclose();
fos = null;
isclose();
is = null;
conncommit();
connsetAutoCommit(true);
connclose();
文件上传到数据库请参考以下示例:
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page language="java" import="javasql" %>
<%/////连接数据库
javasqlConnection conn;
javasqlStatement stmt;
javasqlResultSet rs1;
ClassforName("sunjdbcodbcJdbcOdbcDriver");
conn=DriverManagergetConnection("jdbc:odbc:dababasename");///数据源
stmt=conncreateStatement();
上传文件时,需要给SmartUpload传一个上传文件的保存地址,一般都用一个String来表示,使用request来获得。比如,你要把文件保存在项目路径下的upload文件夹,刚:
String
path
=
requestgetRealPath("upload");//获取upload文
//夹的绝对路径。
//得到:c://项目名/upload/
然后再拼上你的文件名,比如你上传一个叫testtxt的文件,则全部的URL为:
path
+
fileName
=
"c:///项目名/upload/"+"texttxt";
在实际的编程中,其实数据库只需要保存fileName就可以了,path作为配置,每次系统加载时再去读取,这样做的好处是:系统的环境变化时,只需要修改配置文件就能够搞定,而且不会重复保存多余的路径(因为路径都是一样的)
>
以上就是关于怎样能将文件上传到Oracle数据库中全部的内容,包括:怎样能将文件上传到Oracle数据库中、如何确定上传的文件上传到数据库、java ,SmartUpload上传文件后怎么把上传到服务器上的文件路径写入数据库等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)