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

这里的还没有实现功能


一、数据库端操作:
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>

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


**********************************************************************************************
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>
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值