Jsp向MySql數據庫中寫入圖片文件

一、先在數據庫建這樣一張表,下面的這些代碼對任何數據庫都通用,只要支持blob類型的。

         SQL>create   table   image(id  int,content  varchar(200),image   blob);  

二、

testimage.html文件內容如下:    

 <HTML>    

 <HEAD>    

 <TITLE>Image   File   </TITLE>    

 <meta  http-equiv="Content-Type"  content="text/html;  charset=gb2312">    

 </HEAD>    

 <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> 

  我們在Form的action裏定義了一個動作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("org.gjt.mm.mysql.Driver").newInstance();    

 String  url="jdbc:mysql://localhost/mysql?user=root&password=&useUnicode=true&characterEncoding=8859_1";    

  //其中mysql爲你數據庫的名字,user爲你連接數據庫的用戶,password爲你連接數據庫用戶的密碼,可自己改    

 Connection   conn=   DriverManager.getConnection(url);    

 String  content=request.getParameter("content");    

 String  filename=request.getParameter("image");    

 FileInputStream   str=new   FileInputStream(filename);    

 String   sql="insert   into  test(id,content,image)   values(1,?,?)";    

 PreparedStatement  pstmt=dbconn.conn.prepareStatement(sql);    

 pstmt.setString(1,content);    

 pstmt.setBinaryStream(2,str,str.available());    

 pstmt.execute();    

 out.println("Success,You  Have   Insert   an  Image   Successfully");    

  %> 

下面我寫一個測試image輸出的例子看我們上面程序寫的對不對,testimageout.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("org.gjt.mm.mysql.Driver").newInstance();    

 String  url="jdbc:mysql://localhost/mysql?user=root&password=&useUnicode=true&characterEncoding=8859_1";    

  //其中mysql爲你數據庫的名字,user爲你連接數據庫的用戶,password爲你連接數據庫用戶的密碼,可自己改    

 Connection   conn=   DriverManager.getConnection(url);    

 String   sql   =  "select   image   from  test   where   id=1";    

 Statement   stmt=null;    

 ResultSet   rs=null;    

 try{    

 stmt=conn.createStatement();    

 rs=stmt.executeQuery(sql);    

 }catch(SQLException   e){}    

 try   {    

 while(rs.next())   {    

 res.setContentType("image/jpeg");    

 ServletOutputStream   sout   =  response.getOutputStream();    

 InputStream   in   =  rs.getBinaryStream(1);    

 byte   b[]   =  new   byte[0x7a120];    

 for(int   i   =  in.read(b);   i   !=  -1;)    

 {    

 sout.write(b);    

 in.read(b);    

 }    

 sout.flush();    

 sout.close();    

  }    

 }    

 catch(Exception  e){System.out.println(e);}    

 %>    

 </body>    

 </html>    

  你運行這個程序,你就會看到剛纔你寫入美麗的圖片就會顯示在你面前。怎麼樣,用jsp來試試。    

  這種方法把圖片寫到數據庫中會使數據庫在短時間內容量飛漲,會影響性能的,另外一種做法將圖片存上傳到服務器上,    

  在數據庫裏只存放圖片的路徑,這是一個很好的方法。我建議大家採取後面一種方法。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章