【问题标题】:Displaying a mysql image on a jsp page在 jsp 页面上显示 mysql 图像
【发布时间】:2014-01-25 21:38:57
【问题描述】:

我试图在我的 jsp 页面的某个部分显示从 mysql 获取的图像。问题是,无论 代码放在哪里,我都只得到图像,而不是页面上的其余内容。我想在从查询结果返回的图像之上获取导航栏和其他所有内容。 代码:

<%@page import="java.sql.*" %>
<%@page import="java.io.*" %>
<%!public static Connection connect(){
    Connection con = null;
    try {
    Class.forName("com.mysql.jdbc.Driver").newInstance();


    con = DriverManager.getConnection("jdbc:mysql://localhost:3306/Imperia","root", "");
    return con;
    } catch (Exception e) {
        throw new Error(e);
    }
}
public static boolean close (Connection c)
{
    try{
        c.close();
        return true;
    }
    catch (Exception e)
    {
        return false;
    }
}
%>
<%@ page language="java" contentType="text/html; charset=windows-1255"
    pageEncoding="windows-1255"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
    <script src="http://code.jquery.com/jquery-1.10.2.min.js" type="text/javascript"></script>
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link href="css/bootstrap.min.css" rel="stylesheet">
        <link href="css/styles.css" rel="stylesheet">

        <div id="Imperia_Bank">
            <p>Imperia Bank</p>
        </div>
        <div id = "User" >
        <p> Hello: Administrator </p>
        </div>  <div id = "User" >

        </div>
    <script type="text/javascript">

            $(function(){
                $('.fadein img:gt(0)').hide();
                setInterval(function(){
                    $('.fadein :first-child').fadeOut()
                        .next('img').fadeIn()
                        .end().appendTo('.fadein');}, 
                3000);
            });

        </script>

    </head>
    <body>


        <div class="navbar navbar-inverse navbar-static-top">
        <div class="container">

            <a href="#" class="navbar-brand">Imperia Bank</a>

            <button class="navbar-toggle" data-toggle="collapse"
                data-target=".navHeaderCollapse">
                <span class="icon-bar"></span> <span class="icon-bar"></span> <span
                    class="icon-bar"></span>
            </button>

            <div class="collapse navbar-collapse navHeaderCollapse">

                <ul class="nav navbar-nav navbar-right">

                    <li class="active"><a href="homepage.html">Home</a></li>
                    <li><a href="Login.html">Login</a></li>
                    <li><a href="Register.html">Register</a></li>
                    <li><a href="customer.html">Clients</a></li>
                    <li><a href="Administrator.html">Administrator</a></li>
                    <li><a href="account.html">Accounts</a></li>
                    <li><a href="Illustration.html">Illustration</a></li>
                    <li ><a href="ATM.html">Atm</a></li>

                </ul>

            </div>

        </div>
    </div>
    </div>
        <ul id="navigation">

        <div id="red_line_head"></div>
        <div id="main">
            <td id="description">
                <p><p><br><br>Founded in 1992 as a Finance and Securities Company, 
                Imperia Bank converted into a fully fledged commercial bank in January 1996. 
                Since then the bank has had a long standing tradition of achieving strong financial performance and carrying out 
                expansion strategies while successfully focusing on efficient client service delivery.
                Over the last 20 years, Imperia Bank has achieved a sustained growth in our customer deposit base which is largely attributed to the level of confidence our customers have in the bank and our corporate strategy.
                Currently we have 23 branches in our expanding branch network across major towns and cities.<br><br>
                   Imperial Bank will continue to enhance the existing risk management parameters through the effective use of our newly installed and cutting edge core banking system.
                   As part of our growth strategy, we are also continuing to recruit qualified professional staff, and providing appropriate training to our existing human resources to ensure we continue to meet evolving customer demands.
                </p>
            </td>
            <td id="news">
                <p>
                <br><br><br><u>NEWS:</u>
                <br><br>
                        <td>    <div class="fadein">
  <img src="http://farm3.static.flickr.com/2610/4148988872_990b6da667.jpg">
  <img src="http://farm3.static.flickr.com/2597/4121218611_040cd7b3f2.jpg">
  <img src="http://farm3.static.flickr.com/2531/4121218751_ac8bf49d5d.jpg">
</div>


</td>
<table border="0" >
    <tr>    
    </tr>
    <tr>
    <td><%
    Blob image = null;

    Connection con = null;

    byte[] imgData = null;

    Statement stmt = null;

    ResultSet rs = null;

    try {

    con=connect();
        stmt = con.createStatement();

        rs = stmt
                .executeQuery("select Picture from Imperia.homepage where idpicture= '1'");

        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();

            con.close();

        } catch (SQLException e) {

            e.printStackTrace();

        }

    }
 %></td>
        <td><button type="button" id= "Next" onclick= "next()">></button></td>



    </tr>

</table> 

            </td>

    </body>
</html>

这里有什么问题?

【问题讨论】:

    标签: html mysql jsp


    【解决方案1】:

    您需要执行以下操作:

    首先创建一个 servlet,然后在你的 servlet 中连接到数据库并获取图像并写入图像流。

      public class ImageServlet extends HttpServlet {
    
        private static final long serialVersionUID = 1L;
    
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
    
            response.setContentType("image/jpeg");
    
            byte[] imageBytes = getImageAsBytesFromDB();
    
    
            response.setContentLength(imageBytes.length);
    
            response.getOutputStream().write(imageBytes);
    
    
            response.getOutputStream().flush(); 
            out.close();
    
        }
    
    }
    

    第二 - 在您的 html 中执行以下操作:

    <img src="url to your servlet">
    

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-29
      • 2019-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-27
      • 1970-01-01
      相关资源
      最近更新 更多