SSISO Community

시소당

blob형에 파일 저장 및 읽기

[데이터베이스에  파일저장  blob형]
ByteArrayOutputStream  baos  =  new  BtyeArrayOutputStream();
FileInputStream  fis  =  new  FileInputStream(args[0]);
byte[]  buffer  =  new  byte[512];
Int  icount  =0;
while((icount  =  fis.read(buffer))>0){
baos.write(buffer,  0,  icount);
}
byte[]  ibytes  =  baos.toByteArray();
fis.close();
baos.close();
PreparedStatement  ps  con.prepareStatement(sql);
ps.setString(1,  aa);
ps.setByte(2,  ibytes);
  
[데이터베이스에  파일읽기  blob형]
PreparedStatement  ps  =  con.prepareStatement(sql);
ps.setString(1,  aa);
ResultSet  rs  =  ps.executeQuery();
rs.next();
Blob  bfile  =  rs.getBlob(1);
InputStream  is  =  rs.getBinaryStream(1);
FileOuputStream  fos  =  new  FileOutputStream(args[0]);
byte[]  buffer  =  new  byte[512];
Int  icount  =0;
while((icount  =  is.read(buffer))>0){
        fos.write(buffer,  0,  icount);
}
fos.close();
rs.close();
ps.close();

출처  :  http://blog.empas.com/zeroscience/19942882

666 view

4.0 stars