【问题标题】:Issue about displaying Blob(Image) from Mysql table using JSP关于使用 JSP 从 Mysql 表中显示 Blob(Image) 的问题
【发布时间】:2014-05-29 19:42:09
【问题描述】:

我想在 JSP 页面中显示来自 MySQL 表的 blob(image),我的代码如下:

  <%
    Blob image = null;
    Connection con = null;
    Statement stmt = null;
    ResultSet rs = null;
    byte[ ] imgData = null ; 

    String DRIVER= "com.mysql.jdbc.Driver";
    String databaseName = "imd";
    String connectionUrl = "jdbc:mysql://127.0.0.1:3306/" + databaseName;
    String DB_USER = "root";
    String DB_PASSWD = "root";

    try{
    Class.forName(DRIVER);
    con = DriverManager.getConnection(connectionUrl,DB_USER,DB_PASSWD);
    stmt = con.createStatement();
    rs = stmt.executeQuery("select SiteKey from imd_user_sitekey where userName = 'lili'");
    if (rs.next()) {
       image = rs.getBlob(1);
       imgData = image.getBytes(1,(int)image.length());
    } else {
        out.println("Display Blob Example");
        out.println("image not found for given id>");
        return;
    }
    response.setContentType("image/gif");
    OutputStream out = response.getOutputStream();
    out.write(imgData);
    out.flush();
    oout.close();
    }catch (Exception e) {
        out.println("Unable To Display image");
        out.println("Image Display Error=" + e.getMessage());
        return;
        } finally {
        try {
        rs.close();
        stmt.close();
        con.close();
        } catch (SQLException e) {
        e.printStackTrace();
        }
        }

  %> 

 <h1>Hello, <%= message %>, Please enter your password!</h1>
 <br/>
    <html:form action="sitekey">
         <bean:message key="label.password"/>
         <html:password property="password"></html:password>             
        <html:submit/>
    </html:form>

图片可以显示成功,但是我的JSP页面中代码下面的其他内容没有显示,整个页面就像一张图片一样。有人可以帮我解决这个问题吗? 非常感谢!

【问题讨论】:

    标签: jsp


    【解决方案1】:

    因为你设置了response.setContentType("image/gif");。要在您的页面中显示 HTMl,您的 JSP 页面内容类型必须是 response.setContentType("text/html");

    【讨论】:

      【解决方案2】:

      此代码可能对您有所帮助....这是我的 show_image.jsp

      <%
      String id="1250";
      session.setAttribute("num", id);
      %>
        <body>
        <image src="display.jsp" border="0" height="200px" width="200px"/>
      
      
            <h1>Hello, Please enter your password!</h1>
           <br/>
           <html:form action="sitekey">
           <bean:message key="label.password"/>
           <html:password property="password"></html:password>             
          <html:submit/>
          </html:form>
       </body>
      

      这里我通过 dispaly.jsp 文件从外部引用图像

       <% Blob image = null;
       String no=(String)session.getAttribute("num"); 
       byte[ ] imgData = null ;
       Statement stmt = null;
       ResultSet rs = null;
       try {
           Class.forName("com.mysql.jdbc.Driver");
           Connection con = (Connection);      DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","");
           stmt = con.createStatement();
           rs = stmt.executeQuery("select photo from file1 where id = '"+no+"'");
           if (rs.next()) {
           image = rs.getBlob(1);
           imgData = image.getBytes(1,(int)image.length());
           } else {
                  out.println("Display Blob Example");
                  out.println("image not found for given id");
                  return;
                  }
            // display the image
         response.setContentType("image/gif");
         OutputStream o = response.getOutputStream();
         o.write(imgData);
         o.flush();
         o.close();
          } catch (Exception e) {
                                 out.println("Unable To Display image");
                                 out.println("Image Display Error=" + e.getMessage());
                                 return;
                                } finally {
                                          try {
                                               rs.close();
                                               stmt.close();
                                              } catch (SQLException e) {
                                                                       System.out.println(e);
                                                                       e.printStackTrace();
                                                                       }
                               }
             %> 
      

      【讨论】:

        猜你喜欢
        • 2016-08-23
        • 2019-12-30
        • 2014-06-20
        • 1970-01-01
        • 2015-10-13
        • 2011-06-22
        • 1970-01-01
        • 1970-01-01
        • 2013-04-26
        相关资源
        最近更新 更多