• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

displaying byte array as image in IE

 
P. Ingle
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • 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
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64720
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I assume this servlet is being referenced by an <img> tag on the page?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64720
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Btw, please use the UBB CODE tags when posting code.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
tripti pandey
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • 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
Author and ninkuma
Marshal
Pie
Posts: 64720
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There may be a language issue here, but Bear's reply was not meant to be offensive.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64720
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • 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
Author and ninkuma
Marshal
Pie
Posts: 64720
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
thanks
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • 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.
 
dema rogatkin
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic