大文件存储数据库

[align=left][/align][b]项目中有附件上传功能,直接把附件上传后,通过后台将附件直接保存到数据库,附件一早般500M以内,
分享一下给大家:
/**
* 增加文件实体到数据库
*/
public String addFileObj(File file){

/**
* 获取数据库唯一主键
* @return 主键 32位字符窜
* public synchronized static String getId()
* {
* return (String) new UUIDHexGenerator().generate(null,null);
* }
*/
String innerid = PKGenarator.getId();// 获取主键
//大字段插入数据库
String setEmptyBlobSQL = "insert into " + "Sys_File_obj_tab "
+ " (innerid, FILEOBJECT) values ('" + innerid
+ "',empty_blob())";
//从数据取出的sql
String getEmptyBlobSQL = "select t.FILEOBJECT from "
+ " Sys_File_obj_tab " + " t where innerid = '" + innerid
+ "' for update";

}
/**
* jdbc完成将大字段上传到数据库的操作.
*/
ResultSet rs=null;
OutputStream outStream=null //s输出流
FileInputStream in = null;//输入量
int bs = 0;
try{
Connection conn = DBService.getConnection("dbcp_dzkfframe");
Statement stmt = conn.createStatement();
stmt.executeUpdate(setEmptyBlobSQL);
rs = stmt.executeQuery(getEmptyBlobSQL);
rs.next();
oracle.sql.BLOB blo = (oracle.sql.BLOB) rs.getBlob(1);
outStream = blo.setBinaryStream(0);
bs = blo.getBufferSize();
in = new FileInputStream(file);
byte[] buf = new byte[bs];
int length = 0;
while(true){
length=in.read(buf);
if(length==-1){
break;
}
outStream.write(buf,0,length);
}
outStream.close();
outStream = null;
in.close();
in = null;
conn.commit();
conn.setAutoCommit(true);
return innerid;

}catch(Exception ex){
ex.printStackTrace();
return null;

}finally {
DBService.freeConnection();
}

}[/b]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值