0

I have a webapplication for displaying images using imageio on a servlet. The webapp works fine when hosted in tomcat 7 using jre 1.6. But the same webapp when deployed on tomcat 5.5 with servlet 2.4 and Jre 1.5 it doesnt work. To access the image i pass an identifier as parameter and i get the image which is a blob column in DB. Initially i developed the application on tomcat 7 instance using eclipse and it worked fine. Compiler version compatibility is selected as 1.5 and Dynamic web module version is 2.4. In the below code i have checked the jdbc connectivity, it works fine and i can display textual information. Also i have checked separately whether the BufferedImage is having some data by checking for null and it seemed to have data. But the servlet simply fails to showup the image and i just end up getting a blank screen.

works on tomcat 7 [http://localhost:8081/testimage/ReturnImage?code=AUS]

doesnt work on tomcat 5.5 [http://localhost:8080/testimage/ReturnImage?code=AUS]

The servlet just displays a blank screen for the latter. Below is the code for my servlet.

package flags;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
import java.awt.image.BufferedImage;

public class ReturnImage extends HttpServlet {
    private static final long serialVersionUID = 1L;

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

        //PrintWriter out = response.getWriter();
        // OutputStream outimg = response.getOutputStream();

        try {
            Class.forName("com.mysql.jdbc.Driver");

            Connection connection = DriverManager.getConnection(
                    "jdbc:mysql://192.168.2.2:3306/world", "root", "abcdef");

            //out.println("Connecting to database <br>");

            Statement statement = connection.createStatement();
            String param;
                param = request.getParameter("code");

            String sql = "Select Name,Flag,Code from world.Country where Code='"+ param + "'";
            ResultSet res = statement.executeQuery(sql);
            while (res.next()) {
                String Name = res.getString("Name");
                String Code = res.getString("Code");
                BufferedImage image = javax.imageio.ImageIO.read(res.getBlob("Flag").getBinaryStream());
                //out.println(System.getProperty("java.runtime.version"));
                //out.println(Code + " ");
                //out.println(Name + "<br>");
                if (image == null) {
                    //out.println("null image");
                }
                 ImageIO.write(image, "gif", outimg);
                 outimg.close();        
            }
            res.close();
            statement.close();
            connection.close();

        } catch (SQLException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }   
    }       
}

Image shows the servlet output in two different tomcat instances. Tomcat 5.5 uses JDK1.5 and Tomcat 7 uses JDK 1.6

Dhirendra Khanka
  • 759
  • 1
  • 8
  • 21
  • I added response.setContentType("image/gif"); but all i can see now is a box with a cross in it. – Dhirendra Khanka Jun 01 '13 at 11:22
  • I tried repointing the JVM of the tomcat 5.5 to a client jvm.dll of jre 1.6 and now being able to display the image. How can i fix this without reporting it to a newer version. I am asking this because i cannot tinker any such settings on a production box. The older JRE already has the required libraries, so what is exactly failing. Please help!!! – Dhirendra Khanka Jun 01 '13 at 11:50
  • Why are you decoding and encoding the image in the first place? Are you planning to do any image processing with it, add metadata, watermarks or similar? Why not stream the blob directly, along with the proper content type? – Harald K Jun 02 '13 at 20:43

1 Answers1

0

You probably need the setContentType set correctly (as you currently do).

However, have you tried using a different format, like PNG (and of course, use "image/png" as content type)? I'm not sure Java 1.5 has a GIF writer, due to the LZW licensing issues of the past. Note that the ImageIO.write methods have a boolean return type, to check if anything was written. In any case, PNG should always work.

PS: Unless you are planning to modify the image in the servlet, it is of course much faster to store the image in the right format in the blob, and just passing it down to the client without decoding/encoding it.

Harald K
  • 26,314
  • 7
  • 65
  • 111
  • Hi Harald, big thanks for your answer. I found that the Boolean output of ImageIO.write was false for Jre 1.5 and the javadoc says that it returns false if no appropriate writer is found. Since i was wrting a GIF image back to outputstream so it was failing. I then tried outputting all my GIFs as jpeg and its displaying correctly. Only thing left for me to wonder is whether the conversion will always be correct, i have only tested it for 10-20 samples. Also could you advice as to how can i pass my InputStream directly to outputstream without the BufferedImage ? – Dhirendra Khanka Jun 03 '13 at 16:11
  • You're welcome. But you asked the question, so you have to mark it as answered. Please do! – Harald K Jun 03 '13 at 20:33
  • For how to copy input to output, see the code example in this [answer](http://stackoverflow.com/a/1574857/1428606). – Harald K Jun 03 '13 at 20:38
  • My reputation points seems not enough to mark the answer as useful :-) – Dhirendra Khanka Jun 04 '13 at 05:29
  • See this for [How to mark a question answered](http://meta.stackexchange.com/questions/147531/how-mark-my-question-as-answered-on-stackoverflow) – Harald K Jun 04 '13 at 05:50