以二进制流的方式存取图像

imageinfo.sql
  CREATE   TABLE   [dbo].[emp]   (   
  [name]   [varchar]   (
50 )   COLLATE   Chinese_PRC_CI_AS   NULL   ,   
  [pay]   [money]   NULL   ,   
  [img]   [image]   NULL     
  )   ON   [PRIMARY]   TEXTIMAGE_ON   [PRIMARY]  
test.html
   < HTML >      
  
< HEAD >      
  
< TITLE > Image   File    </ TITLE >      
  
< meta   http - equiv = " Content-Type "    content = " text/html;   charset=gb2312 " >      
  
</ HEAD >
  
< h1 > 图片上传 </ h1 >      
  
< FORM   METHOD = POST   ACTION = " testimage.jsp " >      
  
< INPUT   TYPE = " text "    NAME = " content " >< BR >      
  
< INPUT   TYPE = " file "    NAME = " image " >< BR >      
  
< INPUT   TYPE = " submit " ></ FORM >      
  
< BODY >      
  
</ BODY >      
  
</ HTML >
testimage.jsp
   <% @   page   contentType = " text/html;charset=gb2312 " %>      
  
<% @   page    import = " java.sql.* "     %>      
  
<% @   page    import = " java.util.* " %>      
  
<% @   page    import = " java.text.* " %>      
  
<% @   page    import = " java.io.* " %>      
  
< html >      
  
< body >      
  
<%    
  Class.forName(
" com.microsoft.jdbc.sqlserver.SQLServerDriver " );   
  String   url   
=     " jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=imageinfo " ;   
  Connection   conn   
=    DriverManager.getConnection(url, " sa " , " sa " );   
  System.out.println(
" diver   is   ok " );   
    
  String   content
= request.getParameter( " content " );     
  String   filename
= request.getParameter( " image " );     
  FileInputStream   img
= new    FileInputStream(filename);     
  String   sql
= " insert   into   emp(name,pay,img)   values('lyd',12,?) " ;     
  PreparedStatement   pstmt
= conn.prepareStatement(sql);     
  pstmt.setBinaryStream(
1 ,img,img.available());     
  pstmt.execute();     
  out.println(
" Success,You   Have   Insert   an   Image   Successfully " );
  pstmt.close();
  conn.close();
  img.close();     
  
%>    
show.jsp
  <% @   page   contentType = " text/html;charset=gb2312 " %>      
  
<% @   page    import = " java.sql.* "     %>      
  
<% @   page    import = " java.util.* " %>      
  
<% @   page    import = " java.text.* " %>      
  
<% @   page    import = " java.io.* " %>      
  
< html >      
  
< body >      
  
<%    
  Class.forName(
" com.microsoft.jdbc.sqlserver.SQLServerDriver " );   
  String   url   
=     " jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=imageinfo " ;   
  Connection   conn   
=    DriverManager.getConnection(url, " sa " , " sa " );   
  System.out.println(
" diver   is   ok " );   
  String   sql   
=     " select   img   from   emp    " ;     
  Statement   stmt
= null ;     
  ResultSet   rs
= null ;     
  
try {     
  stmt
= conn.createStatement();     
  rs
= stmt.executeQuery(sql);     
  }
catch (SQLException   e)   
  {   
  out.println(
" db   connection   error! " );   
  }     
  
try {     
      
while (rs.next()){   
          
// rs.setContentType("image/jpeg");     
          ServletOutputStream   sout    =    response.getOutputStream();     
          InputStream   in   
=    rs.getBinaryStream( " img " );     
          
byte    b[]    =     new     byte [ 0x7a120 ];     
              
for ( int    i    =    in.read(b);   i    !=     - 1 ;)     
              {     
                  sout.write(b);     
                  in.read(b);     
              }     
          sout.flush();     
          sout.close();  
          in.close();   
     }     
  }     
  
catch (Exception   e){System.out.println(e);}
  
finally {
      rs.close();
      stmt.close();
      conn.close();
      
  }     
  
%>      
  
</ body >      
  
</ html >

以下方法为输出到磁盘上:
package  org.hank.test;

import  java.io. * ;
import  java.sql.Connection;
import  java.sql.DriverManager;
import  java.sql.ResultSet;
import  java.sql.Statement;

public   class  ImageTest {

    
/**
     * 
@param  args
     
*/
    
public   static   void  main(String[] args) {
          String   driver   
=     " com.microsoft.jdbc.sqlserver.SQLServerDriver " ;   
          String   url   
=     " jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=imageinfo " ;   
          String   user   
=     " sa " ;   
          String   password   
=     " sa " ;   
            
          InputStream   in   
=     null ;   
          
try      
          {   
              FileOutputStream   fos
= new    FileOutputStream( " c:\\123.jpg " );   
              Class.forName(driver);   
              Connection   conn   
=    DriverManager.getConnection(url,   user,   password);   
              Statement   stmt   
=    conn.createStatement();   
              String   sql   
=     " select   img   from   emp " ;   
              ResultSet   rs   
=    stmt.executeQuery(sql); 
              System.out.println(rs.toString());
              
if    (rs.next())   
              {   
              in   
=    rs.getBinaryStream( " img " );  
              
int    len;   
              
byte []   buf    =     new     byte [ 1024 ];   
                    
while    ( ( len    =    in.read(buf,    0 ,    1024 ) )    !=     - 1  )   
                  {   
                      fos.write(buf,   
0 ,   len);   
                  }   
              }   

          }   
          
catch    (Exception   exc){}   
            
    }  
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值