469,306 Members | 1,901 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,306 developers. It's quick & easy.

Displaying an image in JSP

Hi,
I have an image, in JPEG format, stored inside a byte array.
I have to render that image. I will not be able to open a file
connection and write that image to HDD, either on client side or on
server side. So my query is this, how do I render an image which is
stored as a series of bytes? I have tried out.println and IMG tag, but
they do not work.

Any help would be appreciated

Bye
Amardeep Verma
Jul 17 '05 #1
6 21514
presumably your JSP page is currently producing HTML. HTML cannot contain
image data in the file itself, only through an external href.

so the best option would be to generate an <img> tag in your JSP which
contains a URL to a different JSP (or the same jsp with different
parameters). When the browser requests the image, you can send the binary
data as content type image/jpeg.

Andy
"Amardeep Verma" <ad******@email.com> wrote in message
news:1a**************************@posting.google.c om...
Hi,
I have an image, in JPEG format, stored inside a byte array.
I have to render that image. I will not be able to open a file
connection and write that image to HDD, either on client side or on
server side. So my query is this, how do I render an image which is
stored as a series of bytes? I have tried out.println and IMG tag, but
they do not work.

Any help would be appreciated

Bye
Amardeep Verma

Jul 17 '05 #2
"Amardeep Verma" <ad******@email.com> wrote in message
news:1a**************************@posting.google.c om...
Hi,
I have an image, in JPEG format, stored inside a byte array.
I have to render that image. I will not be able to open a file
connection and write that image to HDD, either on client side or on
server side. So my query is this, how do I render an image which is
stored as a series of bytes? I have tried out.println and IMG tag, but
they do not work.


I haven't tried this, but my guess is that you could include a tag like this
in your html: <img src="myimage.jsp">. Then have myimage.jsp return the byte
data. Be sure to set the http response header to indicate that it's a jpeg.


Jul 17 '05 #3
On 1 Aug 2003 00:10:55 -0700, ad******@email.com (Amardeep Verma)
wrote or quoted :
So my query is this, how do I render an image which is
stored as a series of bytes?


see http://mindprod.com/jgloss/image.html

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
Jul 17 '05 #4
Chris wrote:
"Amardeep Verma" <ad******@email.com> wrote in message
news:1a**************************@posting.google.c om...
Hi,
I have an image, in JPEG format, stored inside a byte array.
I have to render that image. I will not be able to open a file
connection and write that image to HDD, either on client side or on
server side. So my query is this, how do I render an image which is
stored as a series of bytes? I have tried out.println and IMG tag, but
they do not work.

I haven't tried this, but my guess is that you could include a tag like this
in your html: <img src="myimage.jsp">. Then have myimage.jsp return the byte
data. Be sure to set the http response header to indicate that it's a jpeg.

FYI, since JSPs implicitly open the output stream as a character stream
and the specification prohibits one from re-opening it as a byte stream,
this is best done from a servlet instead of a JSP.

Ray

Jul 17 '05 #5
Allen E. Scharfenberg wrote:
I think you can set the MIME type you are returning in both servlets and
JSP. SO, you could set the MIME type being returned to image\jpeg or
whatever, convert the byte[] to a character[], and then just slam it
through.


No; you can set the MIME type if you like, but a JSP is only allowed to
return character data. There was a tech tip from Sun a while back that
dealt with this very issue. The original tip suggested using JSPs, but
the next tip made the correction. In fact, I just found the tip in my
email library; it wasd the tech tip for 8/21/2001.

Here are the links:
http://developer.java.sun.com/develo...01/tt0821.html
http://developer.java.sun.com/develo...21_update.html

Ray

Jul 17 '05 #6
Allen E. Scharfenberg wrote:[color=blue]
> I think you can set the MIME type you are returning in both servlets and
> JSP. SO, you could set the MIME type being returned to image\jpeg or
> whatever, convert the byte[] to a character[], and then just slam it
> through.
>[/color]

No; you can set the MIME type if you like, but a JSP is only allowed to
return character data. There was a tech tip from Sun a while back that
dealt with this very issue. The original tip suggested using JSPs, but
the next tip made the correction. In fact, I just found the tip in my
email library; it wasd the tech tip for 8/21/2001.

Here are the links:
http://developer.java.sun.com/develo...01/tt0821.html
http://developer.java.sun.com/develo...21_update.html

Ray
Hi Ray

Thanks a lot. You solved my problem. I was thinking along the same lines on setting the mime type on jsp response. These URLs showed how to do it exactly.
Jul 14 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Gregory | last post: by
5 posts views Thread by ljuljacka | last post: by
4 posts views Thread by redpears007 | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.