Oracle中图片的读写

来源:互联网 发布:万网 域名维护费用 编辑:程序博客网 时间:2024/05/01 18:02

static void read() throws SQLException, IOException {
Connection conn = null;
Statement st = null;
ResultSet rs = null;
try {
// 2.建立连接
conn = JdbcUtils.getConnection();
// 3.创建语句
st = conn.createStatement();

// 4.执行语句
rs = st.executeQuery("select big_bit from blob_test");

// 5.处理结果
while (rs.next()) {
InputStream in = rs.getBinaryStream("big_bit");

File file = new File("IMG_0002_bak.jpg");
OutputStream out = new BufferedOutputStream(
new FileOutputStream(file));
byte[] buff = new byte[1024];
for (int i = 0; (i = in.read(buff)) > 0;) {
out.write(buff, 0, i);
}
out.close();
in.close();
}
} finally {
JdbcUtils.free(rs, st, conn);
}
}


上面是读的。。
这个是写进去的

static void create() throws SQLException, IOException {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
// 2.建立连接
conn = JdbcUtils.getConnection();
// conn = JdbcUtilsSing.getInstance().getConnection();
// 3.创建语句
String sql = "insert into blob_test(big_bit) values (?) ";
ps = conn.prepareStatement(sql);
File file = new File("IMG_0002.jpg");
InputStream in = new BufferedInputStream(new FileInputStream(file));

ps.setBinaryStream(1, in, (int) file.length());
// 4.执行语句
int i = ps.executeUpdate();

in.close();

System.out.println("i=" + i);
} finally {
JdbcUtils.free(rs, ps, conn);
}
}

---------------------------------------------------------------------------------------------------------------------------------------

首先建立测试数据表  
    drop   table   filelist;  
    commit;  
     
    CREATE   TABLE   SYSTEM.FILELIST   (    
    "FILENAME"   VARCHAR2(50)   NOT   NULL,  
    "FILESIZE"   NUMBER(20)     NULL,    
    "FILEBODY"   BLOB     NULL,      
    PRIMARY   KEY("FILENAME"),   UNIQUE("FILENAME"))   ;  
    commit;  
   
                测试过程,首先将硬盘文件读入数据库,然后再读出到硬盘的另一个新文件里,原码如下:  
   
  import   java.io.*;  
  import   java.util.*;  
  import   java.sql.*;  
  import   oracle.sql.*;  
  import   oracle.jdbc.driver.*;  
  import   java.text.*;  
  public   class   test  
  {  
    public   static   void   main(String   args[])   throws   java.io.IOException,java.sql.SQLException  
    {  
      dbBean   db1=new   dbBean();    
      /**  
      *数据库连接的JavaBean  
      */  
      byte   a[]=null;//**将测试文件test.doc读入此字节数组  
      java.io.FileInputStream   fin=null;  
      java.io.FileOutputStream   fout=null;  
      oracle.jdbc.OracleResultSet   ors=null;//**这里rs一定要用Oracle提供的  
      oracle.jdbc.driver.OraclePreparedStatement   opst=null;//**PreparedStatement用  
                                                                                                                                                              //Oracle提供的  
         
      try  
      {  
         
        java.io.File   f1=new   java.io.File("c:/temp/test.doc");  
        java.io.File   f2=new   java.io.File("c:/temp/testout.doc");//**从BLOB读出的信息写  
                                                                                                                                    //入该文   件,和源文件对比测试用  
        fin=new   java.io.FileInputStream(f1);  
        fout=new   java.io.FileOutputStream(f2);  
         
         
        int   flength=(int)f1.length();//**读入文件的字节长度  
        System.out.println("file   length::"+flength);  
        a=new   byte[flength];  
         
        int   i=0;int   itotal=0;  
        /**将文件读入字节数组  
        for   (;itotal<flength;itotal=i+itotal   )  
        {  
           
          i=fin.read(a,itotal,flength-itotal);  
           
        }  
        fin.close();  
         
        System.out.println("read   itotal::"+itotal);  
      /**注意Oracle的   BLOB一定要用EMPTY_BLOB()初始化    
      String   mysql="insert   into   filelist   (FileName,FileSize,FileBody)   values   (?,?,EMPTY_BLOB())";  
      opst=(oracle.jdbc.driver.OraclePreparedStatement)db1.conn.prepareStatement(mysql);  
                    opst.setString(1,"wordtemplate");  
                        opst.setInt   (2,flength);  
                    opst.executeUpdate();  
                    opst.clearParameters();  
                    /**插入其它数据后,定位BLOB字段  
                        mysql="select   filebody   from   filelist   where   filename=?";  
                        opst=(oracle.jdbc.driver.OraclePreparedStatement)db1.conn.prepareStatement(mysql);  
                        opst.setString(1,"wordtemplate");  
                        ors=(oracle.jdbc.OracleResultSet)opst.executeQuery();  
                        if   (ors.next())  
                        {  
                         
                        oracle.sql.BLOB   blob=ors.getBLOB(1);/**得到BLOB字段                        
                        int   j=blob.putBytes(1,a);/**将字节数组写入BLOB字段  
                        System.out.println("j:"+j);  
                                               
                        db1.conn.commit();  
                        ors.close();  
                        }                    
                         
            System.out.println("insert   into   ok");  
             
          byte   b[]=null;/**保存从BLOB读出的字节  
          opst.clearParameters();  
                        mysql="select   filebody   from   filelist   where   filename=?";  
                        opst=(oracle.jdbc.driver.OraclePreparedStatement)db1.conn.prepareStatement(mysql);  
                        opst.setString(1,"wordtemplate");  
                        ors=(oracle.jdbc.OracleResultSet)opst.executeQuery();  
                        if   (ors.next())  
                        {  
                        oracle.sql.BLOB   blob2=ors.getBLOB(1);    
                         
                        System.out.println("blob2   length:"+blob2.length());  
                        b=blob2.getBytes(1,flength);/**从BLOB取出字节流数据  
                        System.out.println("b   length::"+b.length);  
                        db1.conn.commit();  
                        }      
                        ors.close();  
                        /**将从BLOB读出的字节写入文件  
                        fout.write(b,0,b.length);  
                        fout.close();      
           
            System.out.println("write   itotal::"+b.length);  
                                     
         
      }  
      catch(Exception   e)  
      {  
        System.out.println("errror   :"+e.toString()   );  
        e.printStackTrace();  
         
      }  
      finally  
      {   /**关闭所有数据联接  
        stmt.close();  
        db1.closeConn();  
      }  
       
       
    }  
  }  
          编译运行在TomCat下调试通过。  
          需要注意的是Blob存取的过程,一般先存入和BLOB相关的控制数据,如文件的名字,  
          然后查询定位BLOB字段,利用OracleBlob提供的方法:  
          public   int   putBytes(long   pos,byte   bytes[])  
          public   byte[]     getBytes(long   pos,byte   bytes[])  
          或者利用  
          public   OutputStream   getBinaryOutputStream()   throws   SQLException  
          public   InputStream     getBinaryStream()   throws   SQLException  

原创粉丝点击