Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 184
  • Last Modified:

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

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
climbingjaffa
Asked:
climbingjaffa
  • 4
  • 3
  • 3
1 Solution
 
objectsCommented:
contnent type should be whatever the type of image is, eg. image/jpeg
0
 
climbingjaffaAuthor Commented:
I tried that but still get no pilot found and brings up a download save dialog with the servlet name in it!
0
 
climbingjaffaAuthor Commented:
Not pilot found for text/gif
Click Save to save the the content of
http://localhost:8092/WebStore/displayimageservlet?picid=1
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
climbingjaffaAuthor Commented:
Do i have to do any out . printlns r anything.
0
 
sudhakar_koundinyaCommented:
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
 
sudhakar_koundinyaCommented:
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
 
sudhakar_koundinyaCommented:
small correction in my last comment

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

should be
  int len= in.read(bt,0,1000);
0
 
sudhakar_koundinyaCommented:
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
 
objectsCommented:
> Basically the problem in your code is this

The existing loop looks fine, what exactly do you think is the problem with it?
0
 
objectsCommented:
> Not pilot found for text/gif

should be image/gif, not text/gif
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 4
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now