您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

把mysql读取的图片显示在jsp 页面上

2024/8/1 4:52:23发布28次查看
先创建数据库 如: create table bcctphoto( photoid int primary key auto_increment, photoname varchar(50) not null, photo blob ); 2把show.jsp放在tomcat的任意目录下. show.jsp作用:从数据库中读出blob,并产生image/jpg. show.jsp文件如下: %@ page c
先创建数据库
如:
create table bcctphoto(
    photoid int primary key auto_increment,
    photoname    varchar(50)    not null,
    photo    blob
);
把show.jsp放在tomcat的任意目录下. show.jsp作用:从数据库中读出blob,并产生image/jpg.
show.jsp文件如下:
string photo_no = request.getparameter(photo_no);
//mysql连接
class.forname(com.mysql.jdbc.driver).newinstance();
string url=jdbc:mysql://localhost:3306/bigdate?user=root&password=mysqladmin;
connection con = drivermanager.getconnection(url);
//oracle连接
//string url=jdbc:oracle:thin@localhost:1521:orcl2;
//user=system;
//password=manager;
//connection con = drivermanager.getconnection(url,user,password);
try{
// 准备语句执行对象
statement stmt = con.createstatement();
string sql = select * from photo where photo_no = + photo_no;
resultset rs = stmt.executequery(sql);
if (rs.next()) {
blob b = rs.getblob(photo_image);
long size = b.length();
//out.print(size);
byte[] bs = b.getbytes(1, (int)size);
response.setcontenttype(image/jpeg);
outputstream outs = response.getoutputstream();
outs.write(bs);
outs.flush();
rs.close();
}
else {
rs.close();
response.sendredirect(./images/error.gif);
}
}
finally{
con.close();
}
%>
把如下文件放在show.jsp的同一目录下.
index.html文件如下:
图像测试
图像测试
异常处理: 如果出现 getoutputstream() has already been called for this response
异常解析:这里是在释放在jsp中使用的对象,会调用response.getwriter(),因为这个方法是和response.getoutputstream()相冲突的!所以会出现以上这个异常。然后当然是要提出解决的办法,其实挺简单的,在使用完输出流以后调用以下两行代码即可:
解决方法:添加代码
out.clear();  out = pagecontext.pushbody(); 
如下代码:
//注意看以下几句的使用  
outs.flush();  outs.close();  outs=null;  response.flushbuffer();  out.clear();  out = pagecontext.pushbody();
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product