• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

displaying byte array as image in IE

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have following method in a servlet. It calls ImgTable.getImage() method which gets Blob data from database, converts it into bytes (using getBytes) and return byte array.
Once I have the byte array in servlet, I just want to display the image.
It is not displaying anything - just shows empty image box with big red X on top left corner.

Please take a look and let me know if I am missing anything.

-------------------------------------------------------------------
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

System.out.println("in doGet method");
try {
ImgTable img = new ImgTable();
// get image data in form of byte array
byte[] buff = img.getImage();
// buff now image data in forma of byte array
if (buff == null) {
System.out.println("failed");
} else {
System.out.println("got byte array of size: " + buff.length);
//display it on browser
response.setContentType("image/jpeg");
ServletOutputStream ostr = response.getOutputStream();
response.setContentLength(buff.length);
ostr.write(buff);
ostr.flush();
ostr.close();
}
} catch (Exception x) {
x.printStackTrace();
}
} // end doGet
 
Sheriff
Posts: 67754
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I assume this servlet is being referenced by an <img> tag on the page?
 
Bear Bibeault
Sheriff
Posts: 67754
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Btw, please use the UBB CODE tags when posting code.
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I did it by writing one byte at a time to the ServletOuputStream (from a file not from a db).

 
P. Ingle
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Bear Bibeault:
I assume this servlet is being referenced by an <img> tag on the page?



Yes - as
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey..were you able to find solution for this problem, i am facing same problem. I have a image as byte array that i need to display, following is the code i wrote in custom tag for the same :

OutputStream o = response.getOutputStream();
o.write(byteImage);
o.flush();
o.close();

In the JSP i am using following code
<img src="displayImage.jsp">

and this displayImage.jsp finally calls the custom tag. However, it seems that img tag is not accepting jsp. My debugger never reaches inside the custom tag implementation.

Please help
 
Bear Bibeault
Sheriff
Posts: 67754
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

<img src="displayImage.jsp">



You are beating yourself in the head with a hammer trying to render binary data from a JSP. You are much better off doing this sort of thing in a servlet.

More info on why JSP is such a poor technology choice can be had in this article.
 
tripti pandey
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
my query basically is can an img tag point to jsp, will jsp be called in that case?
please reply polietly, else dont
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There may be a language issue here, but Bear's reply was not meant to be offensive.
 
Bear Bibeault
Sheriff
Posts: 67754
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"tp",

There aren't many rules that you need to worry about here on the Ranch, but one that we take very seriously regards the use of proper names. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Thanks!
bear
JavaRanch Sheriff
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Tripti Pandey:
my query basically is can an img tag point to jsp, will jsp be called in that case?


yes and yes.
 
Bear Bibeault
Sheriff
Posts: 67754
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What part of my response did you find impolite?
 
tripti pandey
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"tripti",
please re-read Bears request to change your display name.

Display names must contain a first and last name. I recommend you change it back to the original value, since accounts with invalid display names get deleted.

thanks,
Dave.
 
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JSp should work fine, just get hand on out stream and write binary data. let me know if it isn't working. BTW Does anybody have a code for generation an image with text not recognizable by OCR, something similar to Google mail when you do many failed login attempts you start to getting an image.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe it is referred to as 'Capcha' or 'Captcha' and there is a Java API available for it. Unfortunately I don't have any more information at this stage, but tell us how you go.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic