Show image BLOB type of MySQL in a JSP?

0

I have this code but it does not show me anything, as I can do to show the images I have in my database on a jsp page.

mostrar.jsp:

<%@ page import="java.sql.*" %> 
<%@ page import='java.io.InputStream' %> 
<%@ page import='java.io.OutputStream' %> 
<%
    String login = "root";
    String password = "";
    String url = "jdbc:mysql://localhost/dbimagenes";
    Connection conn = null;
    Statement statement = null;
    ResultSet rs = null;
    int nBytes = 0;
%> 
<html><style type="text/css"> 
        <!-- 
        body { 
            background-color: #F5f5f5; 
        } 
        --> 
    </style><body> 
        <h1>Imagen desde MySQL</h1>
        <table>
            <tr><td>
                    <%
                        Class.forName("com.mysql.jdbc.Driver").newInstance();
                        conn = DriverManager.getConnection(url, login, password);
                        statement = conn.createStatement();
                        rs = statement.executeQuery("SELECT imagen FROM t_imagenes where id='2'");
                        try {
                            if (rs.next()) {
                                response.setContentType("image/jpeg");
                                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");
                    %> 
                </td></tr></table>

        <p> Imagen</p> 
        <a href="index.html">PRINCIPAL</a>
    </body>
</html>

The image keep it that way 'insert.jsp':

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@include file="conexion.jsp" %>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Insertar</h1>
        <%
        String nombre=request.getParameter("txtnombre");
        String marca=request.getParameter("txtmarca");
        String imagen=request.getParameter("txtimagen");

        if(nombre!=null && marca!=null && imagen!=null){
            String qry ="insert into t_imagenes(nombre,marca,imagen) values('"+nombre+"','"+marca+"','"+imagen+"')";
            sql.executeUpdate(qry);
            out.print("Datos Registrados "
                    + "<a href='index.jsp'>REGRESAR</a>");

        }else{

        %>
        <form name="frmimagenes" method="post" action="insertar.jsp">
           nombre: <input type="text" name="txtnombre"/><br/>
           marca: <input type="text" name="txtmarca"/><br/>
           imagen: <input type="file" name="txtimagen" value="" size="50" /><br/>
           <input type="submit" value="Guardar">
        </form>

        <%}//else%>
    </body>
</html>

Now I'm not sure if it does not work the way I keep the image or what is happening so it does not show the image or the text

    
asked by Daniel19x 08.12.2016 в 04:47
source

1 answer

1

Try changing to where you show the image with this code:

OutputStream oImage;
try {
    rs = statement.executeQuery("SELECT imagen FROM t_imagenes where id='2'");
    if(rs.next()) {
        byte barray[] = rs.getBytes(1);
        response.setContentType("image/gif");
        oImage=response.getOutputStream();
        oImage.write(barray);
        oImage.flush();
        oImage.close();
    }
}
catch(Exception ex){
    //ex.printStackTrace();
}finally {
    try{
    if(con!=null)
       con.close();
    }catch(Exception ex){
       // ex.printStackTrace();
    }
}
    
answered by 08.12.2016 в 14:26