File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes displaying byte array as image in IE Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "displaying byte array as image in IE" Watch "displaying byte array as image in IE" New topic
Author

displaying byte array as image in IE

P. Ingle
Ranch Hand

Joined: Apr 25, 2005
Posts: 45
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


Thanks,<br />P.Ingle
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61106
    
  66

I assume this servlet is being referenced by an <img> tag on the page?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61106
    
  66

Btw, please use the UBB CODE tags when posting code.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

I did it by writing one byte at a time to the ServletOuputStream (from a file not from a db).



Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
P. Ingle
Ranch Hand

Joined: Apr 25, 2005
Posts: 45
Originally posted by Bear Bibeault:
I assume this servlet is being referenced by an <img> tag on the page?


Yes - as
tripti pandey
Greenhorn

Joined: Mar 09, 2006
Posts: 5
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

Joined: Jan 10, 2002
Posts: 61106
    
  66

<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

Joined: Mar 09, 2006
Posts: 5
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

Joined: Mar 06, 2001
Posts: 13459

There may be a language issue here, but Bear's reply was not meant to be offensive.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61106
    
  66

"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

Joined: Mar 06, 2001
Posts: 13459

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

Joined: Jan 10, 2002
Posts: 61106
    
  66

What part of my response did you find impolite?
tripti pandey
Greenhorn

Joined: Mar 09, 2006
Posts: 5
thanks
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

"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

Joined: Oct 09, 2002
Posts: 294
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.


Tough in space?, <a href="http://tjws.sf.net" target="_blank" rel="nofollow">Get J2EE servlet container under 150Kbytes here</a><br />Love your iPod and want it anywhere?<a href="http://mediachest.sf.net" target="_blank" rel="nofollow">Check it here.</a><br /><a href="http://7bee.j2ee.us/book/Generics%20in%20JDK%201.5.html" target="_blank" rel="nofollow">Curious about generic in Java?</a><br /><a href="http://7bee.j2ee.us/bee/index-bee.html" target="_blank" rel="nofollow">Hate ant? Use bee.</a><br /><a href="http://7bee.j2ee.us/addressbook/" target="_blank" rel="nofollow">Need contacts anywhere?</a><br /><a href="http://searchdir.sourceforge.net/" target="_blank" rel="nofollow">How to promote your business with a search engine</a>
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

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.
 
 
subject: displaying byte array as image in IE