4 Replies Latest reply on Apr 12, 2007 1:39 PM by 807606

    creation of JPG file

    807606
      Hi,

      I have a jsp page that display's data from the database in html table format.Now i have a button called "image" at the bottom.I want that when the user clicks on the image button a jpg file should be reandered to the user that would contain the table data (from the database) that is displayed in the jsp.

      I am fairly new to image creation in java.I know that i need to use the
      response.setContentType("image/jpeg"); to render the jpg back to the user but don't know how to write the data that is displayed in the table in the jsp to this jpg file and generate this file and show back to the user.
      I also know that i need to use a byte [] but dont know how to do this.


      Is there any way to achieve this?Thanks in advance.

      Regards,
      Aditya
        • 1. Re: creation of JPG file
          800322
          First thing: don't use a JSP. Don't you thing that something that's designed to push out HTML is the worst to use for the job?

          Create a servlet that sets the MIME type and streams out the image upon an incoming request. You can embed the call in the IMG tag.
          • 2. Re: creation of JPG file
            800322
            I don't know what you mean with byte[]... you didn't specify where you get that image from.
            • 3. Re: creation of JPG file
              807606
              Hi,

              Regarding the jpg image i need to create the image that would have the database content.

              Regards,
              Aditya
              • 4. Re: creation of JPG file
                807606
                import javax.imageio.*;
                import javax.imageio.IIOImage.*;
                import java.awt.*;
                import java.awt.image.*;
                import java.io.*;
                import java.util.*;
                public class h
                {
                  public static void main( String a[] ) throws Exception
                  {
                    doit( new FileOutputStream( a[ 0 ] ) );
                  }
                
                  static void doit( OutputStream os ) throws Exception
                  {
                    int                 width0 = 800;
                    int                 height0 = 600;
                    BufferedImage       sm0 = new BufferedImage( width0, height0, BufferedImage.TYPE_INT_BGR );
                    Graphics2D          g0 = sm0.createGraphics();
                
                    ImageIO.write( sm0, "jpg", os );
                  }
                
                }