Oracle存储图片
- 获取图片的流
- 先向数据库中存储一个空blob对象
- 查询获取空的blob对象
- 将图片存储blob对象中
public void saveImg() throws Exception{
URL httpUrl=new URL("http://127.0.0.1:8081/TrueTF/imgs/c-icon.png");
HttpURLConnection conn1=(HttpURLConnection) httpUrl.openConnection();
conn1.setRequestMethod("GET");
conn1.setDoInput(true);
conn1.setDoOutput(true);
conn1.setUseCaches(false);
conn1.connect();
InputStream is=conn1.getInputStream();
String sql = "insert into T_TEST_PICTURE(id,pic) values(1,empty_blob())";
int rt = testService.updateSQL(sql);
if( rt > 0){
System.out.println("initialization succeed!!");
String preCursor = "select id,pic from T_TEST_PICTURE where id='1' for update";
List<Map<String,Object>> maps = testService.findSQL(preCursor);
Blob b = (Blob)maps.get(0).get("PIC");
BufferedOutputStream bos = new BufferedOutputStream(b.setBinaryStream(0L));
BufferedInputStream bis = new BufferedInputStream(is);
int bytes;
try {
while((bytes = bis.read()) != -1){
bos.write(bytes);
}
bis.close();
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}