不同的数据库对Blob的操作有差别,因为有些JDBC接口方法有的数据库厂商未实现,一般用
PreparedStatement pstmt = con.prepareStatement("...");
File f = new File("文件名");
FileInputStream fis = new FileInputStream(f);
pstmt.setBinaryStream(1, fis, f.length);
是可行的。
PreparedStatement pstmt = con.prepareStatement("...");
File f = new File("文件名");
FileInputStream fis = new FileInputStream(f);
pstmt.setBinaryStream(1, fis, f.length);
是可行的。
<jsp:useBean id="fileUpload" scope="page" class="com.xh.upload.SmartUpload" />
con.setAutoCommit(false);
try
{
PreparedStatement preStmt=con.prepareStatement("insert into file_fujian (file_id,note,filename,filesize) values(?,EMPTY_BLOB(),?,?)"); //首先针对每个附件产生一条记录
for(int i=0;i<fileUpload.getFiles().getCount();i++)
{
int f_id=0;//附件的id //判断附件是否有效
if(fileUpload.getFiles().getFile(i).isMissing())
{
continue;
} if(fileUpload.getFiles().getFile(i).getSize()<=0)
{
continue;
}
preStmt.setInt(1,info_id);
preStmt.setString(2,fileUpload.getFiles().getFile(i).getFileName());
preStmt.setInt(3,fileUpload.getFiles().getFile(i).getSize());
preStmt.execute();
//取得当前附件的id
Statement tempStmt=con.createStatement();
ResultSet temprs=tempStmt.executeQuery("select fujian_id.CURRVAL from dual");
if(temprs.next())
{
f_id=temprs.getInt(1);
} byte binBytes[] = new byte[fileUpload.getFiles().getFile(i).getSize()];
for (int j=0;j <fileUpload.getFiles().getFile(i).getSize();j++)
{
binBytes[j] = fileUpload.getFiles().getFile(i).getBinaryData(j);
} temprs=tempStmt.executeQuery("select * from file_fujian where id="+f_id);
//将附件写入
if (temprs.next())
{
BLOB blob = ((OracleResultSet) temprs).getBLOB("note");
OutputStream ost = blob.getBinaryOutputStream();
ost.write(binBytes);
ost.flush();
ost.close();
}
temprs.close();
}
con.commit();
flag=true;
}catch(Exception ex)
{
System.out.println(ex.getMessage());
con.rollback();
flag=false;
}