如何在jsp中显示blob图像

时间:2011-07-28 04:03:32

标签: sql jsp blob

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

                }
            }
%>

2 个答案:

答案 0 :(得分:1)

尝试调整以下代码:

<%  
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>  

答案 1 :(得分:0)

我不是一个java开发人员,但如果我理解正确的代码,你试图通过写入包含图像的整个字节数组来通过响应对象输出图像......

我认为正确的做法是:

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