开发者

how to display a blob image in a jsp

开发者 https://www.devze.com 2023-03-23 16:13 出处:网络
//when i display images in jsp page this error is showing... //The image “http://localhost:8080/TestProject/blobrtv1.jsp?n=vipi” cannot be displayed
//when i display images in jsp page this error is showing...

//The image “http://localhost:8080/TestProject/blobrtv1.jsp?n=vipi” cannot be displayed 

//because it contains errors.

//my code is below....

   <%@page import="java.sql.*" %>

<%@page import="java.io.*" %>

<%!Connection con;

    Statement stmt;

    ResultSet rs7;%>


<%  String name = request.getParameter("n");

            out.println("Name" + name);

            try

            {
                byte b;

                byte imgData[] =开发者_运维知识库 null;

                Blob image;


                Class.forName("com.mysql.jdbc.Driver");

                con = DriverManager.getConnection("jdbc:mysql://localhost:3306/customer", 

"root", "root");

                stmt = con.createStatement();

                rs7 = stmt.executeQuery("SELECT (cusPic) from cusinfo where cusName='" + 

name + "'");
                if (rs7.next())

                {
                    out.println("cheking if loop\n");

                    image = rs7.getBlob(1);

                    out.println("\nImage is "+image);

                    imgData = image.getBytes(1, (int) image.length());

                    out.println("\nImgData is"+imgData);

                } else

                {
                    out.println("Display Blob Example");

                    out.println("image not found for given id>");

                    return;
                }
                // display the image

                response.setContentType("image/jpg");

                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
                {
                    rs7.close();

                    stmt.close();

                    con.close();

                } catch (SQLException e)

                {

                    e.printStackTrace();

                }
            }
%>


Try to adapt the following code:

<%  
Connection con = FactoryConnection_SQL_SERVER.getConnection("Organograma");  
Statement stSuper = con.createStatement();  
Statement stSetor = con.createStatement();  

Blob image = null;  
byte[] imgData = null;  

ResultSet rsSuper = stSuper.executeQuery("Select * From funChart where setor = 'mySetor'");  

if (rsSuper.next()) {  
image = rsSuper.getBlob(12);  
imgData = image.getBytes(1, (int) image.length());  
response.setContentType("image/gif");  
OutputStream o = response.getOutputStream();  
//o.write(imgData); //
//o.flush();  
//o.close();  

--[...]  

<table style="margin: 0px; margin-top: 15px;">  
<tr>  
<td id="foto">  
    <img title="<%=rsSuper.getString("Nome").trim()%>" src="<%= o.wite(imageData); o.flush(); o.close(); %>" />  
</td>  
</td>  

<td id="dados">  
<h3><%=rsSuper.getString("Nome")%></h3>  
<p><%=rsSuper.getString("Cargo")%></p>  
<p>Matrícula:<br/><%=rsSuper.getString("Chave")%></p>  
<p>Telefone:<br/><%=rsSuper.getString("Fone")%></p>  
<p>E-Mail:<br/><%=rsSuper.getString("Email")%></p>  
</td>  
</table>  


I am not a java developer but if I understand correctly the code, you are trying to output the image via the response object by writing the whole byte array containing the image...

I think the right way to do it is:

o.write( 'data:image/jpeg;base64,' +
    '/9j/4AAQSkZJRgABAQEASABIAAD/4gv4SUNDX1BST0ZJTEUAAQEAAAvoAAAAAAIAAABtbnRyUkdCIFhZ' +
    'WiAH2QADABsAFQAkAB9hY3NwAAAAAAAAAAAAAAAAAAAAAAAAAAEAAAAAAAAAAAAA9tYAAQAAAADTLQAA' +
    'AAAp+D3er/JVrnhC+uTKgzkNAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABBkZXNjAAABRAAA' +
    'AHliWFlaAAABwAAAABRiVFJDAAAB1AAACAxkbWRkAAAJ4AAAAIhnWFlaAAAKaAAAABRnVFJDAAAB1AAA' +
    'CAxsdW1pAAAKfAAAABRtZWFzAAAKkAAAACRia3B0AAAKtAAAABRyWFlaAAAKyAAAABRyVFJDAAAB1AAA' +
    'CAx0ZWNoAAAK3AAAAAx2dWVkAAAK6AAAAId3dHB0AAALcAAAABRjcHJ0AAALhAAAADdjaGFkAAALvAAA' +
    'ACxkZXNjAAAAAAAAAB9zUkdCIElFQzYxOTY2LTItMSBibGFjayBzY2FsZWQAAAAAAAAAAAAAAAAAAAAA' +
    'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA' .... and so on and so forth (remaining bytes encoded in Base64).);
0

精彩评论

暂无评论...
验证码 换一张
取 消