[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]
分享一下给大家:
/**
* 增加文件实体到数据库
*/
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]