先把文件读取到内存,再以二进制格式保持到数据库中的大字段中(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 {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:oraDB","bigfou","---");
conn.setAutoCommit(false);
stat = conn.createStatement();
stat.executeUpdate("insert into t_video(id,video) values(1,empty_blob())");
rs = stat.executeQuery("select video from t_video where id = 1");
rs.next();
oracle.sql.BLOB blo = (oracle.sql.BLOB)rs.getBlob(1);
os = blo.getBinaryOutputStream();
bs = blo.getBufferSize();
fis = new FileInputStream("D:\\Temp\\MPlayer-CVS-20040808-KK\\mplayer.exe");
byte[] buf = new byte[bs];
int length = 0;
while(true)
{
length = fis.read(buf);
if(length == -1) break;
os.write(buf,0,length);
}
os.close();
os = null;
fis.close();
fis = null;
conn.commit();
conn.setAutoCommit(true);
conn.close();
} catch(Exception ex) {
ex.printStackTrace();
}
}
读大对象
Java code
InputStream is = null;
FileOutputStream fos = null;
byte[] buf = null;
int bs = 0;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:oraDB","bigfou","-");
conn.setAutoCommit(false);
stat = conn.createStatement();
rs = stat.executeQuery("select video from t_video where id = 1");
rs.next();
oracle.sql.BLOB blo = (oracle.sql.BLOB)rs.getBlob(1);
bs = blo.getBufferSize();
buf = new byte[bs];
int length = 0;
is = blo.getBinaryStream();
fos = new FileOutputStream("d:\\test.exe");
while(true) {
length = is.read(buf);
if(length == -1) break;
fos.write(buf,0,length);
}
fos.close();
fos = null;
is.close();
is = null;
conn.commit();
conn.setAutoCommit(true);
conn.close();
...
更新数据的时候,写insert语句的时候,不更新blob字段,blob字段对应的数据用 empty_blob() 代替就行。
(注意:在执行上面那个 sql 之前,一定要把 connection 设置成不自动提交: conn.setAutoCommit(false); )
最后单独出来blob字段:
//把 blob 字段取出来
String sql = "select ANNEX from market_info_collect_t where info_id='"
+ infoId + "' for update ";
Statement stt=null;
stt = conn.createStatement();
rs = stt.executeQuery(sql);
if (rs.next()) {
blob = (oracle.sql.BLOB) rs.getBlob("ANNEX");
outStream = blob.getBinaryOutputStream();
instream = myFile.getContentStream();
byte[] data = new byte[instream.available()];
instream.read(data);
outStream.write(data, 0, data.length);
}
instream.close();
outStream.flush();
outStream.close();
上面这段是我以前写过的代码中的一部分,你看一下,希望对你能有帮助。。。
可以用blob或bfile类型来存储该类二进制数据。具体说明如下: ★ blob:用来存储可变长度的二进制数据,blob数据在数据库之间或在客户机与服务器进程之间传递,yVSkyV