Servlet to stream image using IMG tag

Discussion in 'Java' started by jsguru72, Feb 21, 2009.

  1. jsguru72

    jsguru72 Guest

    Using some examples I found on the web, I put together the following
    servlet to stream an image to a web page.

    private void doGet( HttpServletRequest req, HttpServletResponse
    res )
    throws ServletException, IOException {

    ServletContext sc = getServletContext();
    String filename = "/public/web/testimage.jpg";

    String mimeType = sc.getMimeType(filename);
    if (mimeType == null) {
    sc.log("Could not get MIME type of "+filename);
    res.setStatus
    (HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
    return;
    }
    res.setContentType(mimeType);

    File file = new File(filename);
    res.setContentLength((int)file.length());

    FileInputStream in = new FileInputStream(file);
    OutputStream out = res.getOutputStream();

    System.out.println("File Name: " + filename );
    System.out.println("MIME Type: " + mimeType );
    System.out.println("File Length: " + (int)file.length());

    byte[] buf = new byte[1024];
    int count = 0;
    while ((count = in.read(buf)) >= 0) {
    System.out.print(".");
    out.write(buf, 0, count);
    }
    in.close();
    out.close();
    }


    The following lines in my web.xml file are related to this servlet.

    <servlet>
    <servlet-name>Image Servlet</servlet-name>
    <servlet-class>testApp.ImageServlet</servlet-class>
    </servlet>

    <servlet-mapping>
    <servlet-name>Image Servlet</servlet-name>
    <url-pattern>/image/*</url-pattern>
    </servlet-mapping>

    <mime-mapping>
    <extension>jpg</extension>
    <mime-type>image/jpeg</mime-type>
    </mime-mapping>

    If I use the URL of /testApp/image/1, it processes the servlet fine
    and I get the image in my browser.
    http://servername:8080/testApp/image/1

    However, if I put "/testApp/image/1" as the src in an IMG tag inside
    the jspx file, I get nothing.
    <img src='/testApp/image/1' border='2' alt='my image'/>


    I have the servlet setup to log some information in the catalina.out
    log. Here is that output.
    File Name: /public/web/testimage.jpg
    MIME Type: image/jpeg
    File Length: 42946
    ...........................................

    File Name: /public/web/testimage.jpg
    MIME Type: image/jpeg
    File Length: 42946
    ...........................................


    The first occurence is when I run this servlet directly from the URL.
    The second is when I associate it with the IMG tag. Note they are
    both identical which would indicate that the servlet is executing
    fine, it just seems that the output is getting lost when I try to put
    this in the IMG tag.

    Any help would be appreciated.

    Thanks.
     
    jsguru72, Feb 21, 2009
    #1
    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. Michael Stock
    Replies:
    1
    Views:
    595
    Joe Gass
    Dec 3, 2003
  2. news.austin.rr.com

    how to download img from html img tag

    news.austin.rr.com, Apr 27, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    512
    Steve C. Orr [MVP, MCSD]
    Apr 27, 2005
  3. shruds
    Replies:
    1
    Views:
    829
    John C. Bollinger
    Jan 27, 2006
  4. Pawel
    Replies:
    2
    Views:
    978
    William Brogden
    Apr 12, 2006
  5. =?utf-8?B?6L65IOaxnw==?=

    [Servlet] how to send a stream to servlet doPost

    =?utf-8?B?6L65IOaxnw==?=, Sep 14, 2007, in forum: Java
    Replies:
    3
    Views:
    646
    GArlington
    Sep 14, 2007
Loading...

Share This Page