我想在JSP页中显示存储在ORACLE数据库中的图片
并且想在一个页面显示多张,即对应数据库中的多条记录值.
另外:请注意,我用的struts框架,希望大家所给出解决方式也是采用struts标签来作,同时希望给出具体的代码示例。多谢多谢!500分!,如果你解决了,请到java板块的其他小类(web开发,j2se基础类,j2se扩展类,j2EE,框架开源)里面可以看到我的同样的帖子,就可以接分了,谢谢!!!

解决方案 »

  1.   

    package name.aspx;import java.io.*;
    import java.sql.*;
    import javax.servlet.*;
    import javax.servlet.http.*;public class ImageServlet extends HttpServlet  
    {
    public void doGet(HttpServletRequest req,HttpServletResponse res) throws IOException,ServletException
    {
    try
    {
    String id = (String)req.getParameter("id");
    Class.forName("org.gjt.mm.mysql.Driver");
    //String url="jdbc:mysql://localhost/jspdev?user=root&password=";
    String url="jdbc:mysql://localhost/xiaohuozhisql?user=xiaohuozhisql&password=6737038";
    Connection conn = DriverManager.getConnection(url);
    Statement stmt = conn.createStatement();
    ResultSet rs = stmt.executeQuery("select * from image where id = " + id);
    if(rs.next())
    {
    InputStream in = rs.getBinaryStream("image");
    OutputStream out = res.getOutputStream();
    byte[] bytes = new byte[1024];
    while(in.read(bytes,0,1024) != -1)
    {
    out.write(bytes);
    }
    }
    conn.close();
    }
    catch(Exception e)
    {
    System.out.println(e);
    }
    } public void doPost(HttpServletRequest req,HttpServletResponse res) throws IOException,ServletException
    {
    }
    }这一个是读敢mysql中的图片,你可以把他改成oracle的。
    然后在jsp调用就可以读了。
      

  2.   

    为什么不用在数据库中存储图片路径的方法.
    1.在BusinessBean中读出数据库中的路径
    ArrayList arrImgPath = new ArrayList();
    while(rs.next()){
       arrImgPath.add(rs.getString("Img_Path"));
    }
    request.setAttribute("imgpath",arrImgPath);
    2.在jsp中调用BusinessBean showImg.显示
    <%@ taglib uri="struts-logic" prefix="logic" %>
    <logic:iterate id="element1" name="element" scope="request">
       <html:img  page="/showImg.do"  paramId="img" paramName="element1" border="1"  width="108" height="26" scope="request"/>
    </logic:iterate>
    3.BusinessBean showImg.java:
    String imgPath=request.getParameter("img"); 
    file = new File(imgPath);
    if (!file.isDirectory()&& file.exists()){
       length = (int)file.length();
    }
    res.setContentType("image/jpg");//假设是jpg图
    res.setHeader("Content-Disposition","inline");
    res.setContentLength(length);
    if (length > 0){
    int bufferSize = 32768;
    InputStream in = new FileInputStream(fullpath);
    ServletOutputStream outfile = res.getOutputStream();
    int chunkLen = bufferSize;
    if(chunkLen > length && length > 0)
       chunkLen = length;
    byte ioBuf[] = new byte[chunkLen];
    do{
    chunkLen = in.read(ioBuf);
    if(chunkLen <= 0)
      break;
    outfile.write(ioBuf, 0, chunkLen);
    length -= chunkLen;
    } while(length != 0);
    in.close();
    outfile.flush();
    outfile.close();
    }