jsp 画面读取mysql数据的图片

afufeng 2011-01-20 10:56:01
我用的是strust1.2+hibernate架构来实现的,现在已经实现了了图片的上传,并以2进制的格式保存到数据里面了。
如何实现把图片在jsp上面显示出来 求助!!
上传的过程大概是这样的,
首先hibernate xml映射里面这样写的

Comic.hbm.xml
........
<property name="coverPaper" type="binary">
<column name="coverPaper" />
</property>
........
Comic.java 文件里面是这样定义的

...........
private byte[] coverPaper;

public byte[] getCoverPaper() {
return coverPaper;
}

public void setCoverPaper(byte[] coverPaper) {
this.coverPaper = coverPaper;
}

.........
上传实现用的action方法
c.setUserType(sendForm.getUserType());
c.setComicType(Short.parseShort(sendForm.getComicType()));
c.setReadOrder(Short.parseShort(sendForm.getReadOrder()));
c.setStatus(Short.parseShort("1"));
c.setSubject(sendForm.getSubject());
Date dt=new Date();
c.setCreatedTime(dt);
c.setUpdatedTime(dt);
//c.setCoverPaper(new byte[0]);
//new Dao().comicinsert(c);
Hashtable files = sendForm.getMultipartRequestHandler().getFileElements();//取得页面上的file
if(files.keys()!=null){
for(Enumeration enu=files.keys();enu.hasMoreElements();){
String key=(String)enu.nextElement();
try{
FormFile formFile=(FormFile)files.get(key);
c.setCoverPaper(formFile.getFileData());

}catch(Exception ee){
ee.printStackTrace();
}

}
}
new Dao().comicinsert(c);

以上就是上传时候大致代码,现在我要实现图片在jsp画面上的显示,
我自己定义了一个Action,showImageAction()用来实现读取DB2进制流,方法如下:
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.liyutec.struts.action;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.liyutec.hibernate.Comic;
import com.liyutec.hibernate.ComicDAO;

/**
* MyEclipse Struts
* Creation date: 01-17-2011
*
* XDoclet definition:
* @struts.action validate="true"
*/
public class ShowImageAction extends Action {
/*
* Generated Methods
*/

/**
* 下载文件方法
* @param mapping
* @param form
* @param request
* @param response
* @return
* @throws Exception
*/
public ActionForward excute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
try{
String id=request.getParameter("id");
BufferedInputStream bis = null;
String fileName=null;
Comic c = null;
if(id==null){
bis=new BufferedInputStream(new ByteArrayInputStream(c.getCoverPaper()));
// fileName = new String(c.getFileRealName().getBytes(), "iso8859-1");
} else{
c=(Comic)new ComicDAO().findById(Long.valueOf(id));
bis=new BufferedInputStream(new ByteArrayInputStream(c.getCoverPaper()));
// fileName=new String(c.getFileRealName().getBytes(), "iso8859-1");
}
response.reset();
response.setContentType("application/x-download");
response.addHeader("Content-Disposition", "attachment;filename=" + fileName);
OutputStream out=response.getOutputStream();
byte[] buffer = new byte[5120];
int size = 0;
while ((size = bis.read(buffer, 0, buffer.length)) != -1) {
out.write(buffer, 0, size);
//System.out.println("---------------"+ImageUtil.getImageType(buffer));
}
}catch(Exception e){
e.printStackTrace();
}
return null;
}

}
jsp显示图片的代码如下:
<dd class="img"><span><img src='<%=request.getContextPath() %>/jsp/showImage.do?id=${c.comicId} alt="图片格式错误" ' /></span></dd>
现在的问题是load到INDEX画面的时候没有调用到jsp/showImage.do这个action
求教如何实现!

...全文
97 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
afufeng 2011-01-23
  • 打赏
  • 举报
回复
呵呵,2种方法,各有各的好处。
问题解决了 我把JSP 掉Action的地方改成调一个servlet了就行了,写了个servlet
哎 不知道为什么不能掉Action,网上看到有人就这么写的。
总之问题解决了先凑合用着。

修改前
<span><img src='<%=request.getContextPath() %>/jsp/showImage.do?id=${c.comicId} alt="图片格式错误" ' /></span></
修改后
<span><img alt="图片格式错误" src="<%=request.getContextPath()%>/ImageServlet?id=${c.comicId}" /></span>
readme1988 2011-01-22
  • 打赏
  • 举报
回复
。。。。。我觉得数据库存图片路径比较好解决
afufeng 2011-01-21
  • 打赏
  • 举报
回复
求解啊 2天了,怎么才能让图片在jsp里显示出来啊
afufeng 2011-01-20
  • 打赏
  • 举报
回复
是这样的 我在找找看看
李大冬瓜 2011-01-20
  • 打赏
  • 举报
回复
现在的问题是load到INDEX画面的时候没有调用到jsp/showImage.do这个action
求教如何实现!

楼主自己都这么说了,请求没有到大ACTION,肯定是servlet或者action路径配置出错或者src访问的路径错误

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧