如何在jsp中显示blob图像

发布于 2024-11-27 02:33:55 字数 2244 浏览 4 评论 0原文

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

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

                }
            }
%>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

晨曦÷微暖 2024-12-04 02:33:55

尝试修改以下代码:

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

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>  
棒棒糖 2024-12-04 02:33:55

我不是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).);

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).);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文