Display images with "<IMG SRC=" where "SRC" is a Java servlet. I got it working for JPG's, but not GIF's.

I'm an intermediate level Java servlet programmer (i.e, I know just enough to get by, without delving into the details).  Anyhoo, I have images that I want only my servlets to display to the browser.  The code I have written below (it's a stripped down version for this posting) works just fine when displaying JPG images via the "<IMG SRC..." tags that are written in my PrintWriter output.  

But for the life of me, I can't find equivalent code that renders GIF files.  I've heard of the Jimi and ACME environments, but do I really need to implement an entire graphics environment for this seemingly easy task??   (And besides, I've downloaded both those packages, and STILL can't seem to get it to work!).

If possible, can someone simply provide the equivalent code as what I've offered below, but to work with GIF images.  I've tried long enough, so need some help.



package mypackage;

import javax.servlet.*;
import javax.servlet.http.*;
import com.sun.image.codec.jpeg.*;
import java.awt.image.BufferedImage;
import java.io.*;

public class JpegView extends HttpServlet {

  private final static String IMG_TOKEN = "img";

  public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    String fName = request.getParameter( IMG_TOKEN );
    File f = new File( "/home/content/m/y/a/myaccountfolder/non_public_folder/" + fName );

    JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(new FileInputStream(f));
    BufferedImage image =decoder.decodeAsBufferedImage() ;
    ServletOutputStream sos = response.getOutputStream();
    JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(sos);

  public static String ImgSrc( String fName ) {
    return "<IMG SRC=\"http://www.-----.com/servlet/mypackage.JpegView"
                     + "?" + IMG_TOKEN + "=" + fName + "\">";
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Tommy BraasCommented:
You are not setting the content type for the response.
stockblocksAuthor Commented:
orangehead911 (regarding your comment on not setting content type in response)...

  I agree -- it should be added.   But the JPG's are still rendered even with that line excluded.  I'm not sure why... perhaps my browser is smart enough to assume what it is.  But I'm getting off track. I'm more interested in figuring out how to get similar code working with GIF's.

stockblocksAuthor Commented:

Your reference to ch09-05.htm was the info I needed.  I adapted it to read from a file (versus database retrieval that page pertained to), and it works.  It's just as simple as I had hoped for.

FYI, the code I used to test it is below.

For kicks, I recompiled with the code WITHOUT calling response.setContentType("image/gif"), and the GIF file still displayed properly!  Why is this?  Same question with my original JPG code (i.e., I didn't include setContentType("image/jpg"), and yet the JPG files were still displayed).  Does this make sense?  I presume it would be wise to include those lines anyway.   Please let me know your thoughts on this.

Thanks again.



  public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    String fName = request.getParameter( IMG_TOKEN );
    File f = new File( ...my disk path...  + "/" + fName );
    int fileSize = (int)f.length();

 // response.setContentType("image/gif");    <--- GIF rendered even WITHOUT this line

    ServletOutputStream out = response.getOutputStream();

    BufferedInputStream gifData = new BufferedInputStream( new FileInputStream( f ) );

    byte[] buf = new byte[4 * 1024];  // 4K buffer
    int len;
     while ((len = gifData.read(buf, 0, buf.length)) != -1) {
        out.write(buf, 0, len);
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java EE

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.