利用JSP存取图片,数据库采用mysql转载

Java代码 复制代码 收藏代码
  1. 一、数据库端操作:   
  2. 1 在mysql下建一个数据库名字叫 testpic        
  3. ===>    
  4. mysql>create database testpic;   
  5.   
  6. 2 在testpic库下建一数据表test,只有两字段    
  7. ===>    
  8. mysql>use testpic;   
  9.                                              
  10. ===>    
  11. mysql>create table test (id int, pic blob);  
一、数据库端操作:
1 在mysql下建一个数据库名字叫 testpic     
===> 
mysql>create database testpic;

2 在testpic库下建一数据表test,只有两字段 
===> 
mysql>use testpic;
                                          
===> 
mysql>create table test (id int, pic blob);


二、相关的html jsp文件
**********************************************************************************************
登录界面   postblob.html
Java代码 复制代码 收藏代码
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">   
  2. <html>   
  3. <head>   
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">   
  5. <title>Insert title here</title>   
  6. </head>   
  7. <body>   
  8. <center>   
  9. <form action="testblob.jsp" method="post" >   
  10. <table width="291" border="1">   
  11.   <tr>   
  12.     <td width="107">id </td>   
  13.     <td width="168"><input name="id" type="text" /></td>   
  14.   </tr>   
  15.   <tr>   
  16.     <td>file</td>   
  17.     <td><input name="file" type="file" /></td>   
  18.   </tr>   
  19.   <tr>   
  20.     <td><input  type="submit"  value="提交"/></td>   
  21.       
  22.   </tr>   
  23. </table>   
  24. </form>   
  25. </center>   
  26. </body>   
  27. </html>  
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<center>
<form action="testblob.jsp" method="post" >
<table width="291" border="1">
  <tr>
    <td width="107">id </td>
    <td width="168"><input name="id" type="text" /></td>
  </tr>
  <tr>
    <td>file</td>
    <td><input name="file" type="file" /></td>
  </tr>
  <tr>
    <td><input  type="submit"  value="提交"/></td>
   
  </tr>
</table>
</form>
</center>
</body>
</html>

**********************************************************************************************
  readblob.jsp界面源码
Java代码 复制代码 收藏代码
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.     pageEncoding="UTF-8"%>   
  3.     <%@ page import="java.sql.*, javax.sql.*" %>   
  4. <%@ page import="java.util.*"%>   
  5. <%@ page import="java.text.*"%>   
  6. <%@ page import="java.io.*"%>    
  7. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">   
  8. <html>   
  9. <head>   
  10. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">   
  11. <title>Insert title here</title>   
  12. </head>   
  13. <body>   
  14. <%   
  15.     
  16.  java.sql.Connection conn;   
  17.  ResultSet rs=null;   
  18.   Class.forName("com.mysql.jdbc.Driver").newInstance();    
  19.    conn= java.sql.DriverManager.getConnection("jdbc:mysql://localhost/testpic","root","root");    
  20.    Statement stmt=conn.createStatement();    
  21.    rs=stmt.executeQuery("select * from test where id=1");   
  22.   if(rs.next())   
  23.   {   
  24.     Blob b = rs.getBlob("pic");   
  25.       
  26.  int size =(int)b.length();   
  27.       out.print(size);   
  28.   InputStream in=b.getBinaryStream();   
  29.   byte[] by= new byte[size];   
  30.   response.setContentType("image/jpeg");    
  31.   ServletOutputStream sos = response.getOutputStream();   
  32.      int bytesRead = 0;   
  33.        while ((bytesRead = in.read(by)) != -1) {   
  34.              sos.write(by, 0, bytesRead);   
  35.           }   
  36.          in.close();   
  37.          sos.flush();   
  38.        
  39.   }   
  40.      
  41.     
  42. %>   
  43. </body>   
  44. </html>  
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ page import="java.sql.*, javax.sql.*" %>
<%@ page import="java.util.*"%>
<%@ page import="java.text.*"%>
<%@ page import="java.io.*"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
 
 java.sql.Connection conn;
 ResultSet rs=null;
  Class.forName("com.mysql.jdbc.Driver").newInstance(); 
   conn= java.sql.DriverManager.getConnection("jdbc:mysql://localhost/testpic","root","root"); 
   Statement stmt=conn.createStatement(); 
   rs=stmt.executeQuery("select * from test where id=1");
  if(rs.next())
  {
    Blob b = rs.getBlob("pic");
   
 int size =(int)b.length();
      out.print(size);
  InputStream in=b.getBinaryStream();
  byte[] by= new byte[size];
  response.setContentType("image/jpeg"); 
  ServletOutputStream sos = response.getOutputStream();
     int bytesRead = 0;
       while ((bytesRead = in.read(by)) != -1) {
             sos.write(by, 0, bytesRead);
          }
         in.close();
         sos.flush();
    
  }
  
 
%>
</body>
</html>


**********************************************************************************************
testblob.jsp界面源码
Java代码 复制代码 收藏代码
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.     pageEncoding="UTF-8"%>   
  3.     <%@ page import="java.sql.*" %>   
  4. <%@ page import="java.util.*"%>   
  5. <%@ page import="java.text.*"%>   
  6. <%@ page import="java.io.*"%>    
  7. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">   
  8. <html>   
  9. <head>   
  10. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">   
  11. <title>Insert title here</title>   
  12. </head>   
  13. <body>   
  14. <%    
  15.  String id=request.getParameter("id");   
  16.  String file=request.getParameter("file");   
  17.  out.print(id);   
  18.  out.print(file);   
  19.  FileInputStream str=new FileInputStream(file);   
  20.  out.print(str.available());   
  21.    java.sql.Connection conn;    
  22.    java.lang.String strConn;    
  23.    Class.forName("com.mysql.jdbc.Driver").newInstance();    
  24.    conn= java.sql.DriverManager.getConnection("jdbc:mysql://localhost/testpic","root","root");    
  25.  String sql="insert into test(id,pic) values(?,?)";    
  26.  PreparedStatement pstmt=conn.prepareStatement(sql);    
  27.  pstmt.setString(1,id);   
  28.  pstmt.setBinaryStream(2,str,str.available());    
  29. pstmt.execute();    
  30. out.println("Success,You Have Insert an Image Successfully");   
  31.  pstmt.close();   
  32. %>    
  33. <a href="readblob.jsp">查看图片</a>   
  34. <a href="postblob.html">返回</a>   
  35. </body>   
  36. </html> 
posted @ 2011-03-24 17:41  huajian168  阅读(718)  评论(0编辑  收藏  举报