如何在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();
}
}
%>
//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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试修改以下代码:
Try to adapt the following code:
我不是java开发人员,但如果我正确理解代码,您将尝试通过写入包含图像的整个字节数组来通过响应对象输出图像...
我认为正确的方法是:
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: