?
Solved

I am trying to display an image from an mysql database using a servlet

Posted on 2005-04-03
10
Medium Priority
?
181 Views
Last Modified: 2010-04-01
I am using the following code and have the content type set to 'text/html":


When i select the link to show product : I get note pilot found and it asks me to save the servlet to disk and my program hangs .   Also want to display this in a jsp rather than a servlet any ideas?

Class.forName(jdbcDriver);
               Connection con = java.sql.DriverManager.getConnection(url);
               Statement stmt = con.createStatement();
               String query = "SELECT image FROM file_test";
               ResultSet rs1 = stmt.executeQuery(query);
               if (rs1.next()){
                  ServletOutputStream o= res.getOutputStream();
                  InputStream in=rs1.getBinaryStream(1);
                  //response.setContentLength(3187);


                  byte[] bt = new byte [1000];
                  for(int i = in.read(bt); i != -1; )
                  {
                     o.write(bt);
                     in.read(bt);
                  }
                  o.flush();
                  o.close();

              }
              con.close();


         }
         catch (Exception e){
              e.printStackTrace();
}
0
Comment
Question by:climbingjaffa
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
10 Comments
 
LVL 92

Expert Comment

by:objects
ID: 13691015
contnent type should be whatever the type of image is, eg. image/jpeg
0
 
LVL 2

Author Comment

by:climbingjaffa
ID: 13691220
I tried that but still get no pilot found and brings up a download save dialog with the servlet name in it!
0
 
LVL 2

Author Comment

by:climbingjaffa
ID: 13691238
Not pilot found for text/gif
Click Save to save the the content of
http://localhost:8092/WebStore/displayimageservlet?picid=1
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 2

Author Comment

by:climbingjaffa
ID: 13691252
Do i have to do any out . printlns r anything.
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 13691747
Try like this

res.setContentType("image/gif");
ServletOutputStream out = res.getOutputStream();
     
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(
  "SELECT IMAGE FROM PICTURES WHERE IMAGEID = " +
req.getParameter("IMAGEID"));

if (rs.next()) {
  BufferedInputStream gifData =
    new BufferedInputStream(rs.getBinaryStream  
      ("IMAGE"));
  byte[] buf = new byte[4 * 1024];  // 4K buffer
  int len;
  while ((len = gifData.read(buf, 0, buf.length))
      != -1) {
    out.write(buf, 0, len);
  }
}
else {
  res.sendError(res.SC_NOT_FOUND);
}
0
 
LVL 14

Accepted Solution

by:
sudhakar_koundinya earned 2000 total points
ID: 13691752
Basically the problem in your code is this

                 byte[] bt = new byte [1000];
                  for(int i = in.read(bt); i != -1; )
                  {
                     o.write(bt);
                     in.read(bt);
                  }
___________________________________________
Should be something like this
___________________________________________

                 byte[] bt = new byte [1000];
                  while(true)
                  {
                    int len= in.read(bt,1000);
                     if(len==-1) break;  
                     o.write(bt,0,len);

                  }
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 13691755
small correction in my last comment

  int len= in.read(bt,1000);

should be
  int len= in.read(bt,0,1000);
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 13691765
Anyhow check this one

http://www.unix.org.ua/orelly/java-ent/servlet/ch09_05.htm from which I have posted the code

Regards
Sudhakar
0
 
LVL 92

Expert Comment

by:objects
ID: 13693926
> Basically the problem in your code is this

The existing loop looks fine, what exactly do you think is the problem with it?
0
 
LVL 92

Expert Comment

by:objects
ID: 13693950
> Not pilot found for text/gif

should be image/gif, not text/gif
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

New style of hardware planning for Microsoft Exchange server.
If you're a modern-day technology professional, you may be wondering if certifications are really necessary. They are. Here's why.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month8 days, 21 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question