• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to fetch a picture from DB?  RSS feed

 
Daniel Washington
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my work,I store pictures as Image type in SQL server.
Now i wanna fetch the pic and show it in JSP,somebody told me that I should
make the pic to a byte array first,so what is next ?
How can I do to make the byte array show as a gif in Jsp?

thx a lot !!!
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hmmmmm
you can use a helper class which may has a method which converts a byte[] into a file and place it into a particular directory, somewhere under /WEB_INF/, then just specify the name, including path, in your jsp.

offcourse you will have the name and extension of that particular image with you in the database, right.
 
Julian Kennedy
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Daniel,

Here is a simple example that I recently posted in this thread.



Do it in a Servlet, not in a JSP.

Hope that helps.

Jules
 
Daniel Washington
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry,it seems your code doesn't work,your codes fetch byte array from a real my-image.jpg and show it using servlet,but I mean how to show the picture which is stored in Blob type.
thx anyway

Originally posted by Julian Kennedy:
Hi Daniel,

Here is a simple example that I recently posted in this thread.



Do it in a Servlet, not in a JSP.

Hope that helps.

Jules
 
Julian Kennedy
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, is your question how to read a Blob type from a database? If you look at the thread I linked to, that may help you. If you give it a bit of thought you can easily adapt my example to work for you.
 
Daniel Washington
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
very thx to Jules,I have already figured out this problem with your help.
now I post my complete JSP code ,hope can help others.
<%@ page contentType="text/html; charset=ISO-8859-1" %>
<%@ page import="java.sql.*,java.io.*"%>

<%
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn = DriverManager.getConnection("jdbc dbc:testforwebsphere","sa","sa");
String sql = "select * from testforphoto where test='111'";
PreparedStatement pstmt = conn.prepareStatement(sql);
ResultSet rs =pstmt.executeQuery();
rs.next();
InputStream in = rs.getBinaryStream("photo");
OutputStream sos = response.getOutputStream();

int len = 0;
byte[] b = new byte[1024];
response.reset();
response.setContentType("image/gif");
while((len = in.read(b)) > 0)
sos.write(b);
in.close();
sos.close();
%>
<html>
<body>
show the picture successfully!
</body>
</html>

Originally posted by Julian Kennedy:
So, is your question how to read a Blob type from a database? If you look at the thread I linked to, that may help you. If you give it a bit of thought you can easily adapt my example to work for you.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!