jsp图片动态存储到mysql数据库中

本文介绍了一种使用JSP将图片动态存储到MySQL数据库的方法,包括创建数据库表、在JSP中连接数据库、上传图片、从数据库读取图片并显示。由于在实际操作中存在性能问题和编码冲突,作者最终放弃了直接存储图片到数据库的方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

        一,创建数据库,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


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值