- 一、数据库端操作:
- 1 在mysql下建一个数据库名字叫 testpic
- ===>
- mysql>create database testpic;
- 2 在testpic库下建一数据表test,只有两字段
- ===>
- mysql>use testpic;
- ===>
- 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
- <!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>
<!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界面源码
- <%@ 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>
<%@ 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界面源码
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- <%@ page import="java.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>
- <%
- String id=request.getParameter("id");
- String file=request.getParameter("file");
- out.print(id);
- out.print(file);
- FileInputStream str=new FileInputStream(file);
- out.print(str.available());
- java.sql.Connection conn;
- java.lang.String strConn;
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- conn= java.sql.DriverManager.getConnection("jdbc:mysql://localhost/testpic","root","root");
- String sql="insert into test(id,pic) values(?,?)";
- PreparedStatement pstmt=conn.prepareStatement(sql);
- pstmt.setString(1,id);
- pstmt.setBinaryStream(2,str,str.available());
- pstmt.execute();
- out.println("Success,You Have Insert an Image Successfully");
- pstmt.close();
- %>
- <a href="readblob.jsp">查看图片</a>
- <a href="postblob.html">返回</a>
- </body>
- </html>