我给你一个源码吧,当时为做一个项目时用到这个把我也给难为住了,后来总算解决了。下面是我写的一个用于显示图片的jsp,你再你的jsp中引用它就行了,这样引用:
<img name="picshow" width="100" height="133" src="showImage.jsp?id=<%=id%>" title="图像预览"/>,其中的参数id是显示图片的jsp即showImage.jsp中用于唯一标志某个图片的一个参数,在showImage.jsp中要用这个id参数将所要显示的图片读出来,我用的库是postgresql的,mysql应该也一样的,如果你是以二进制形式存储的话。showImage.jsp中开始的数据库连接部分你应该改一下,我是tomcat连接池的,估计与你的不一样。

解决方案 »

  1.   

    showImage.jsp:<%@page contentType="text/html;charset=GBK" language="java" %>
    <%@ page buffer="200kb" %>
    <%@taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
    <%@taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
    <%@page import="java.util.*" %>
    <%@page import="java.sql.*" %>
    <%@page import="javax.naming.*" %>
    <%@page import="com.wellsoon.police.db.*" %>
    <%@page import="java.io.*"%>     
    <%    
    try  {  
    String  id=request.getParameter("id");

    //此处联数据库  
    Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        try {
            Context initCtx = new InitialContext();
            Context ctx = (Context) initCtx.lookup("java:comp/env");
            javax.sql.DataSource ds = (javax.sql.DataSource) ctx.lookup("jdbc/police");
            conn = ds.getConnection();
            //stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
        }
        catch (Exception e) {
            System.out.println(e.toString());
        }
    String  cmd="select case when photo is null then (select photo from tab_prephoto where id='aaa') else photo end from tab_photo WHERE  id  ='"+ id + "'";
    System.out.println("cmd.................=" + cmd);
    stmt=conn.prepareStatement(cmd);    
    rs  =  stmt.executeQuery(); byte[] Buffer=new byte[1024*10];
    InputStream InData=null;
    OutputStream outData=null;
    int iSize;
    if(rs.next())
    {
    outData=response.getOutputStream();
    InData=rs.getBinaryStream("photo");
    response.setContentType("image/gif,jpeg");
    while(true)
    {
    iSize=InData.read(Buffer);
    if(iSize==-1)
    {
    break;
    }
    outData.write(Buffer,0,iSize);
    }
    outData.flush();
    response.flushBuffer();
    }else {};
    rs.close();
    InData.close();
    outData.close();try {
    stmt.close();
    conn.close();
      }
      catch (Exception e1) {
    out.println(e1.toString());
      }
      finally {
    try {
      if(stmt != null) stmt.close();
      if(!conn.isClosed()) conn.close();
    }
    catch (Exception e2) {
      out.println(e2.toString());
    }
      }

    catch(Exception  e)  
    {  System.out.println("error while close connection = "+e.toString());}  
     
      
    %>
      

  2.   

    谢谢你,不过我在jbuilder X中make showImage.jsp时,报错: "showImage.jsp": org.apache.jasper.JasperException: File "/WEB-INF/struts-bean.tld" not found
    请问怎么解决啊?
      

  3.   

    哦,这样啊,提示是说struts标签库找不到,我当时是用struts结构做开发的,不过这个页面用不着,你把
    <%@taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
    <%@taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
    这两行删掉。再编译,其中的select 语句要改成你自己的查询语句,引用时的width和height自己调整。