从MySql数据库读取图片

本文介绍了一个使用JSP从数据库检索并显示图片的示例。通过连接数据库并执行SQL查询,可以将图片路径提取出来,并利用Java IO操作读取图片文件,最终将其作为响应发送给客户端进行展示。

create table image( content varchar(10) , image varchar(50) );


testimage.jsp

<%@       page       contentType= "text/html;charset=gb2312"%>         
    <%@       page       import= "java.sql.* "       %>         
    <%@       page       import= "java.util.* "%>         
    <%@       page       import= "java.text.* "%>         
    <%@       page       import= "java.io.* "%>
    <%@ page import="javax.servlet.http.*,javax.servlet.*" %>         
    <html>         
    <body>         
    <%Class.forName( "com.mysql.jdbc.Driver");         
    String url= "jdbc:mysql://localhost:3306/school";           
    Connection conn= DriverManager.getConnection(url,"root","root");                   
    String sql = "select image from image where content='1.jpg'";         
    Statement stmt=null;         
    ResultSet rs=null;         
    response.setContentType("application/jpg");
    response.setHeader("Content-disposition","attachment;filename=sample.jpg"); 
    out.clearBuffer(); 
    out = pageContext.pushBody();        
    ServletOutputStream sout =  response.getOutputStream();
    BufferedInputStream buf=null;
    FileInputStream in=null;
    File file=null;       
    try{         
    stmt=conn.createStatement();
    rs=stmt.executeQuery(sql);
    while(rs.next()){
    String path=rs.getString("image");
    file=new File(path);         
    in=new FileInputStream(file);
    buf=new BufferedInputStream(in);
    int bytes=0;
    while((bytes=in.read())!=-1){
      sout.write(bytes);
    }
    }     
    conn.close();
    stmt.close();                 
    }catch(SQLException e){
       System.out.println(e);
     }
     catch(IOException e){
       System.out.println("Ing!");
     }finally{
     if(sout!=null)
       sout.close();
       if(buf!=null)
       buf.close();
     }
    %>
    </body>         
    </html>      

testimage.html:
<HTML>             <HEAD>         
    <TITLE>Image File</TITLE>         
    <meta http-equiv= "Content-Type" content= "text/html;charset=gb2312">         
    </HEAD>         
    <BODY>
    <img src="http://localhost:8080/ServletTest/testimage.jsp" />                
    </BODY>         
</HTML>  


本人亲测,可行!结果如上:

 当然要完成一次插入多个图片也就好办了:(如下 ,完成插入4张图片)

数据库image内容:

------------------------------------

content    image

------------------------------------

1.jpg        C:\images\1.jpg

2.jpg        C:\images\2.jpg

3.jpg        C:\images\3.jpg

4.jpg        C:\images\4.jpg

------------------------------------

<%@ page language="java" pageEncoding="gb2312" %>
<%@ page contentType="text/html;charset=gb2312" %>
<%request.setCharacterEncoding("gb2312");%>
<%@ page import="java.util.*,java.lang.*,java.sql.*" %>
<%@ page errorPage="error.html" %>
<html>
<head><meta http-equiv=Content-Type content="text/html;charset=gb2312"></head>
<body>
<%int i=1;
while(i<5){
 String srcstring="http://localhost:8080"+request.getContextPath()+"/testimageout.jsp?id="+Integer.toString(i); %>
<img border="1" src="<%=srcstring %>" alt="ok" >
<%i++; }%>
</body></html>

这时只要在testimageout.jsp页面中获取一个参数即可:

String id=request.getParameter("id");

String sql = "select image from image where content like '"+id+"%'";

效果如下:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

itzyjr

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值