mysql 读取图片_从MySql数据库读取图片 | 学步园

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

testimage.jsp

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();

}

%>

testimage.html:

Image File

    testimage.jsp                

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

1353486677_1833.jpg

当然要完成一次插入多个图片也就好办了:(如下 ,完成插入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

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

while(i<5){

String srcstring="http://localhost:8080"+request.getContextPath()+"/testimageout.jsp?id="+Integer.toString(i); %>

ok

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

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

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

效果如下:

1353563951_7330.jpg

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值