隨筆-51  評論-14  文章-0  trackbacks-0
          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){}   
                      
              }  
          }


          posted on 2008-05-07 00:03 Hank1026 閱讀(498) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 嘉兴市| 大同县| 印江| 东丰县| 南召县| 响水县| 防城港市| 荆州市| 广宁县| 云林县| 抚远县| 松阳县| 开封县| 玉溪市| 皋兰县| 杭州市| 巴中市| 丹凤县| 汕尾市| 扶余县| 荃湾区| 麻栗坡县| 赣榆县| 亳州市| 万载县| 稻城县| 泽普县| 张家界市| 梨树县| 若尔盖县| 长顺县| 仲巴县| 马龙县| 柘城县| 仙桃市| 广河县| 大城县| 枣庄市| 辽宁省| 靖边县| 盘山县|