如何在JSP中显示来自MySQL(BLOB)的图像?

时间:2017-02-22 21:49:29

标签: java mysql jsp

您好我想在JSP中显示图像,并且能够在div,类中处理它,希望如图像ej:<img src="image from MySQ">

我有以下代码,但它以全屏显示图像。

<%Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection(url, login, password);
statement = conn.createStatement();
rs = statement.executeQuery("SELECT photo FROM contacts where contact_id='1'");
try {
    if (rs.next()) {
        response.setContentType("image");
        InputStream is = rs.getBinaryStream(1);
        OutputStream aux = response.getOutputStream();
        out.println("jajaja");
        byte[] buffer = new byte[4096];
        for (;;) {
            nBytes = is.read(buffer);
            if (nBytes == -1) {
                break;
            }
            aux.write(buffer, 0, nBytes);
        }
        is.close();
        aux.flush();
        aux.close();
    } else {
        throw new SQLException("image not found");
    }
    rs.close();
} catch (SQLException e) {
    out.println("Imagen no encontrada");
}
out.println("no se muestra");%>

1 个答案:

答案 0 :(得分:0)

这是我的代码:

<%@ page import="java.sql.*" %> 
<%@ page import='java.io.InputStream' %> 
<%@ page import='java.io.OutputStream' %> 
<%
    String login = "root";
    String password = "secret";
    String url = "jdbc:mysql://localhost/test";
    Connection conn = null;
    Statement statement = null;
    ResultSet rs = null;
    int nBytes = 0;
%> 
<html>
    <body>
        <%
                        Class.forName("com.mysql.jdbc.Driver").newInstance();
                        conn = DriverManager.getConnection(url, login, password);
                        statement = conn.createStatement();
                        rs = statement.executeQuery("SELECT photo FROM contacts where contact_id='1'");
                        try {
                            if (rs.next()) {
                                response.setContentType("image");
                                InputStream is = rs.getBinaryStream(1);
                                OutputStream aux = response.getOutputStream();
                                out.println("jajaja");

                                byte[] buffer = new byte[4096];
                                for (;;) {
                                    nBytes = is.read(buffer);
                                    if (nBytes == -1) {
                                        break;
                                    }
                                    aux.write(buffer, 0, nBytes);
                                }
                                is.close();
                                aux.flush();
                                aux.close();
                            } else {
                                throw new SQLException("image not found");
                            }
                            rs.close();
                        } catch (SQLException e) {
                            out.println("Imagen no encontrada");
                        }
                        out.println("no se muestra");
                    %>     
<h1>My Image from DB</h1>
    <div>
       <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgWETRHHFIDIDIDCNCNNRRLKJPPDJDdjfSSS==" alt="My Image from DB" />
    </div>
        <p> Imagen</p> 
        <a href="index.html">PRINCIPAL</a>
    </body>
</html>