hibernate + sql servler 的图片存储(2)

//  action for image show

package capinfo.negroup.cms.controller.action.infopub.columnmag;

import java.io.ByteArrayInputStream;
import javax.servlet.ServletOutputStream;
import capinfo.negroup.cms.model.CMSColumnBean;
import capinfo.negroup.cms.entity.CMSColumn;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletRequest;

public class GetPic {

  public void getPic(String id, HttpServletRequest request,
                     HttpServletResponse response) {
    try {
      int iid = Integer.parseInt(id);
      CMSColumnBean dao = new CMSColumnBean(iid);
      CMSColumn column = dao.getColumn();
      byte[] image = column.getImg();
      if (image == null || image.equals(null) || image.length == 0) {
      }
      else {
        ByteArrayInputStream in = new ByteArrayInputStream(image);
        response.setContentType("image/jpeg");
        ServletOutputStream sout = response.getOutputStream();
        int len = image.length;
        byte[] buf = new byte[len];
        int c = 0;
        while ( (c = in.read(buf, 0, len)) != -1) {
          sout.write(buf, 0, c);
        }
        sout.flush();
        sout.close();
      }
    }
    catch (Exception ex) {
      System.out.println("Erro here " + ex);
    }

  } 
}

// jsp page that invokes the SHOWPIC action

<html>
<head>
<title>
showpiccommon
</title>
</head>
<body bgcolor="#ffffff">
<h1>
<jsp:useBean id="picBean" class="capinfo.negroup.cms.controller.action.infopub.columnmag.GetPic"/>
<%picBean.getPic((String)session.getAttribute("id"),request,response);%>
</h1>
</body>
</html>


posted @ 2004-11-22 18:51  黄金¤小草  阅读(165)  评论(0)    收藏  举报