Displaying an image in JSP

Discussion in 'Java' started by Amardeep Verma, Aug 1, 2003.

  1. 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
     
    Amardeep Verma, Aug 1, 2003
    #1
    1. Advertising

  2. Amardeep Verma

    Andy Fish Guest

    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" <> wrote in message
    news:...
    > 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
     
    Andy Fish, Aug 1, 2003
    #2
    1. Advertising

  3. Amardeep Verma

    Chris Guest

    "Amardeep Verma" <> wrote in message
    news:...
    > 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.
     
    Chris, Aug 1, 2003
    #3
  4. Amardeep Verma

    Roedy Green Guest

    Roedy Green, Aug 2, 2003
    #4
  5. Chris wrote:
    > "Amardeep Verma" <> wrote in message
    > news:...
    >
    >>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
     
    Raymond DeCampo, Aug 9, 2003
    #5
  6. 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/developer/JDCTechTips/2001/tt0821.html
    http://developer.java.sun.com/developer/JDCTechTips/2001/tt0821_update.html

    Ray
     
    Raymond DeCampo, Aug 16, 2003
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. http://www.visual-basic-data-mining.net/forum

    Displaying image after selecting an image file.

    http://www.visual-basic-data-mining.net/forum, Sep 15, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    357
    http://www.visual-basic-data-mining.net/forum
    Sep 15, 2004
  2. Michael

    JSP displaying wrong page.

    Michael, Sep 2, 2003, in forum: Java
    Replies:
    0
    Views:
    323
    Michael
    Sep 2, 2003
  3. Robert Mark Bram
    Replies:
    0
    Views:
    3,956
    Robert Mark Bram
    Sep 28, 2003
  4. Replies:
    0
    Views:
    4,435
  5. Replies:
    1
    Views:
    399
    Andrew Thompson
    Oct 28, 2006
Loading...

Share This Page