oralc 中图片的存储与读取

来源:互联网 发布:黛安芬淘宝旗舰店真假 编辑:程序博客网 时间:2024/06/07 16:51

 

ai0709编写,joker_yao整理

//数据类型 long row
//存储
SaveImg
public void insertr_save_img(String img_no,String imgpath) throws Exception{
    PreparedStatement pstmt 
= null;
    
try{
        
        File file 
= new File(imgpath); 
        
int fileLength =(int) file.length(); 
        InputStream fin 
= new FileInputStream(file); 
        
        
                
        String query 
= "Insert into save_img( " +
                              
"img_no, emp_pict"+
                       
" ) values ( "+
                              
"?, ?)";
        pstmt 
= connection.prepareStatement(query);
        pstmt.setString(
1,img_no);
        pstmt.setBinaryStream (
2, fin, fileLength); 
             
int affected = pstmt.executeUpdate();
      
    }
catch(Exception e){
        
    }
    
finally {
        
try{pstmt.close();}catch(Exception e){}
    } 
// try-finally

// end insert

//读取
Readimg
public byte[]  select_img(String emp_no) throws Exception{
    PreparedStatement pstmt 
= null;
    
byte[] bo;
    ResultSet rs 
= null;
    
try{
        String query 
= "Select  emp_pict " +
                       
"  from save_img" +
                       
"  where serial_no =?  ";
        pstmt 
= connection.prepareStatement(query);
        pstmt.setString(
1,emp_no); 
        rs 
= pstmt.executeQuery();
       
if(rs.next()) { 
           bo
=rs.getBytes(1);
        } 
// end While
        else { 
            bo
=new byte[0];}
    } 
finally {
        
try{rs.close();}catch(Exception e){}
        
try{pstmt.close();}catch(Exception e){}
    } 
// try-finally
        return bo;
// end select
//jsp显示

<%     
                    
        
try{   
              Class.forName(
"oracle.jdbc.driver.OracleDriver").newInstance();         
             String   url
="jdbc:oracle:thin:@127.0.0.1:1521:oracle?      user=scott&password=tiger&useUnicode=true&characterEncoding=8859_1";         
            Connection   conn
=   DriverManager.getConnection(url);         
            String   sql   
=   "select emp_pict from ee.ba020m where emp_no="+emp_no_imge;         
            Readimg img
= new Readimg(conn);
            String img_no
="1";
            
byte[] bo = img.select_img(img_no);     
            
              response.reset();     
            response.setContentType(
"image/jpeg");    
            
            ServletOutputStream   sout   
=   response.getOutputStream();     
            
               
for(int i= 0 ; i< bo.length; i++){   
                           sout.write(bo[i]);   
                }     
              sout.flush();     
              sout.close();     
                  
          }     
          
catch(Exception   e){System.out.println(e);}  
          
finally{
          
try{rs.close();}catch(Exception e){}
              
if ( conn!= null ) conn.close();
          }   
      
%>     
原创粉丝点击