当前位置:我的异常网» 编程 » oracle 存BLOB跟读取
oracle 存BLOB跟读取
www.myexceptions.net 网友分享于:2013-09-26 浏览:55次
oracle 存BLOB和读取
public void savaSoftFile(InputStream inputStream,String userid) {
// 传的是存入数据库图片的id
Connection conn=getConn();
Statement st = null;
BLOB blob = null; // 图片类型
OutputStream outputStream = null; // 输出流
File file = null; // 文件
ResultSet rs = null;
try {
conn.setAutoCommit(false); // 事物由程序员操作
st = conn.createStatement();
st.executeUpdate( "update SOFTDEPFILE set DEPFILE = empty_blob() where ID='"
+ userid + "'");
rs = st.executeQuery("select DEPFILE from SOFTDEPFILE where id='"
+ userid + "' ");
if (rs.next()) {
blob = (BLOB) rs.getBlob(1);
outputStream = blob.getBinaryOutputStream();
byte[] b = new byte[blob.getBufferSize()];
int len = 0;
while ((len = inputStream.read(b)) != -1) {
System.out.println(len);
outputStream.write(b, 0, len);
}
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
inputStream.close();
outputStream.flush();
outputStream.close();
rs.close();
st.close();
conn.commit();
conn.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void writeImg(OutputStream os,String userid) {
try {
Connection conn=getConn();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select uploadpic from pub_usertable where userid='"
+ userid + "'");
byte[] b = new byte[1024];
if (rs.next()) {
Blob blob = rs.getBlob(1);
InputStream is = blob.getBinaryStream();
int i = 0;
while ((i = is.read(b)) != -1) {
os.write(b, 0, i);
}
os.close();
is.close();
}
rs.close();
stmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
文章评论