一,创建数据库,create tables images(id int not null primary key ,content varchar(100),image blod );
二,在jsp中连接数据库,
import java.sql.Connection;
import java.sql.DriverManager;
public class Conn {
final String MYSQLDBDRIVER="org.gjt.mm.mysql.Driver";
final String MYSQLDBURL="jdbc:mysql://localhost:3306/userinfo";
final String MYSQLDBUSER="root";
final String MYSQLDBUSERPASS="";
public Connection getConnection()
{
try{
Class.forName(MYSQLDBDRIVER);
Connection con=DriverManager.getConnection(MYSQLDBURL,MYSQLDBUSER,MYSQLDBUSERPASS);
return con;
}catch(Exception e){
e.printStackTrace();
return null;
}
}
}
三,在jsp页面中上传图片到mysql中
<form method="post" action="image.jsp" >
<input type="file" name="image" /><br/>
相片描述<input type="text" name="content" /><br/>
<input type="submit" />
</form>
image.jsp页面如下:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<%@ page import="java.util.*"%>
<%@ 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=ISO-8859-1">
<title>图片上传</title>
</head>
<body>
<jsp:useBean id="conn4" class="com.conDBMS.Conn" scope="session" />
<%
String content=request.getParameter("content");
String filename=request.getParameter("image");
//ServletInputStream str=request.getInputStream();
InputStream str=new FileInputStream(filename);
//byte b[]=new byte[str.available()];
//str.read(b);
//ByteArrayInputStream bi=new ByteArrayInputStream(b);
String sql1="select id from image";
Statement stmt=conn4.getConnection().createStatement();
ResultSet rs=stmt.executeQuery(sql1);
rs.last();
int id=rs.getInt("id")+1;
String sql="insert into image(id,content,ima) values("+id+",?,?)";
PreparedStatement pstmt=conn4.getConnection().prepareStatement(sql);
pstmt.setString(1,content);
long l=(long)str.available();
pstmt.setBinaryStream(2,str,l) ;
//out.print(bi.available());
try{
pstmt.executeUpdate();
}catch(Exception e){
//out.print(sql);
out.print( e.getMessage());
}
out.println("Success,You Have Insert an Image Successfully");
%>
</body>
</html>
四,在html页面中显示数据库中的图片
<jsp:useBean id="conn6" class="com.conDBMS.Conn" scope="session" />
<% Statement stmt=conn6.getConnection().createStatement();
String sql=new String();
sql= "select * from image";
ResultSet rs=stmt.executeQuery(sql);
rs.last();
%>
<table>
<tr><td><IMG height=99 src="downloadImage.jsp?id=1" width=136></td></tr>
<tr><td><IMG height=99 src="downloadImage.jsp?id=<%=rs.getInt("id") %> " width=136></td>
</tr>
</table>
五,jsp页面读取mysql中的图片,重要api为rs.getBinaryStream();
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="utf-8"%>
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*"%>
<html>
<body>
<jsp:useBean id="conn5" class="com.conDBMS.Conn" scope="session" />
<%
Statement stmt=conn5.getConnection().createStatement();
ResultSet rs=null;
//建立ResultSet(结果集)对象
int id= Integer.parseInt(request.getParameter("id"));
//获得所要显示图片的编号id,并转换为整型
String sql = "select ima from image WHERE id="+id+"";
//要执行查询的SQL语句
rs=stmt.executeQuery(sql);
while(rs.next()) {
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();
}
%>
</body>
</html>
六,总结
在eclipse中运行时读出图片时数据库的开销特别大,导致电脑死机,所以本人放弃这种直接将图片存在mysql中的方法,而是改用在数据库中存储图片的索引,图片内容存在硬盘中,在操作中还出现这样的一个问题,当时我的mysql数据库的编码为GBK,而在jsp页面中调用rs.getBinaryStream(i,input,input.availoable())时,出现sqlsynax什么的异常,结果要把mysql的字符集改为gb2312