随笔-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 阅读(485) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: