Class.forName(“Oracle.jdbc.driver.OracleDriver”);
Connection con = DriverManager.getConnection(
“jdbc:oracle:thin:@localhost:1521:orcl”, “111”, “111”);
//处理事务
con.setAutoCommit(false);
Statement st = con.createStatement();
//插入一个空对象
st.executeUpdate(“update cc_ac03 set picture=empty_blob() where aac002='”+idcard+”‘”);
//用for update方式锁定数据行
ResultSet rs = st.executeQuery(
“select picture from cc_ac03 where aac002='”+idcard+”‘”);
if (rs.next()) {
//得到java.sql.Blob对象,然后Cast为oracle.sql.BLOB
oracle.sql.BLOB blob = (oracle.sql.BLOB) rs.getBlob(1);
//到数据库的输出流
OutputStream outStream = blob.getBinaryOutputStream();
// ByteArrayInputStream bis=upload.getFiles().getFile(0).getContentStream();
//这里用一个文件模拟输入流
// File file = new File(picture);
// InputStream fin = new FileInputStream(file);
// SmartUpload su = new SmartUpload();
//将输入流写到输出流
byte[] b = new byte[blob.getBufferSize()];
int len = 0;
while ( (len = stream.read(b)) != -1) {
outStream.write(b, 0, len);
//blob.putBytes(1,b);
}
//依次关闭(注意顺序)
// fin.close();
outStream.flush();
outStream.close();
con.commit();
con.close();
}
解决的心得就是一定要把流文件存入数据库,而不是存本机的东东,不然就会去读本机的东西了。其实解决非常简单,过程却是痛苦的
ByteArrayInputStream stream = upload.getFiles().getFile(0).getContentStream();这儿就已经得到流文件的值啦