将mysql中的Blob的图片在jsp中显示
2012-09-06 11:45
260 查看
本来我的例子是用Struts2+Hibernate+Spring三个框架搭建的,但是没有什么大碍,我们只有知道核心部分在怎么实现的就行。
以下是我的Action的代码:
//show image
private static Image image = new Image();
public Image getImage() {
return image;
}
public void setImage(Image image) {
this.image = image;
}
//imageDao对象我是Spring注入进来的,你可以用new创建
public static void showImage(){
byte[] itemImage = null;
HttpServletResponse response = ServletActionContext.getResponse();
response.reset();
response.setContentType("image/jpeg");
try {
image.setId(0);
itemImage = imageDao.getImage(image).getBytes(1, (int)imageDao.getImage(image).length()); //通过getBytes()方法将获取的Blob类型数据转换
OutputStream out = response.getOutputStream();
out.write(itemImage);
out.flush();
out.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
以下是ImageDAO的method
public Blob getImage(Image image) {
Image img = (Image) template.get(Image.class, image.getId()); //按指定id获取的image对象
Blob blob = img.getImage();
return blob; //return blob 对象
}
JSP的代码为:
<img src="http://wewoor.blog.163.com/blog/<s:url value="show" />" border="0" width="100" height="100">本文出自 “Ziv小威” 博客,请务必保留此出处http://zivxiaowei.blog.51cto.com/8815385/1397729
以下是我的Action的代码:
//show image
private static Image image = new Image();
public Image getImage() {
return image;
}
public void setImage(Image image) {
this.image = image;
}
//imageDao对象我是Spring注入进来的,你可以用new创建
public static void showImage(){
byte[] itemImage = null;
HttpServletResponse response = ServletActionContext.getResponse();
response.reset();
response.setContentType("image/jpeg");
try {
image.setId(0);
itemImage = imageDao.getImage(image).getBytes(1, (int)imageDao.getImage(image).length()); //通过getBytes()方法将获取的Blob类型数据转换
OutputStream out = response.getOutputStream();
out.write(itemImage);
out.flush();
out.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
以下是ImageDAO的method
public Blob getImage(Image image) {
Image img = (Image) template.get(Image.class, image.getId()); //按指定id获取的image对象
Blob blob = img.getImage();
return blob; //return blob 对象
}
JSP的代码为:
<img src="http://wewoor.blog.163.com/blog/<s:url value="show" />" border="0" width="100" height="100">本文出自 “Ziv小威” 博客,请务必保留此出处http://zivxiaowei.blog.51cto.com/8815385/1397729
相关文章推荐
- 将mysql中的Blob的图片在jsp中显示
- 将mysql中的Blob的图片在jsp中显示
- struts2从数据库中读取mysql的Blob格式图片和sqlserver中image格式图片显示到jsp上
- JSP读取Oracle数据库里的图片Blob字段并显示在页面上
- 用于显示ORACLE_BLOB存储的图片、flash文件的JSP文件,用于直接下载文件的JSP
- 在jsp中显示数据库Blob字段图片实例
- ssh框架向数据库添加blob图片以及在jsp中显示blob图片
- java读取mysql中保存的图片并显示在jsp上
- struts2中使用Blob类型处理图片上传保存在数据库中并在JSP页面中显示图片
- 如何在jsp页面中显示BLOB保存的图片数据
- 从Oracle中数据库中读取以Blob类型存储的图片显示在jsp页面中
- oracle blob 图片jsp显示
- JSP页面直接显示Blob类型图片
- ThinkPHP读取excel中的图片并以blob类型存入mysql,以及从mysql读取二进制流在页面显示图片
- java以Blob形式存储,读取图片并在jsp页面显示图片流
- MySQL中BLOB存放的图片在页面中显示
- java以Blob形式存储,读取图片并在jsp页面显示图片流
- 基于struct2实现保存图片到mysql的blob字段和显示
- (springmvc)从oracle读取blob类型图片并在jsp中显示
- 把mysql读取的图片显示在jsp 页面上