I didn't mean for this to become debate. I was stuck with a legacy app
that required storing images in the db.

With the help of this list and some digging I think I came up with a
relatively elegant solution (subject to expert critique which is
welcome). I don't know if attachments are allowed on this list so I've
included the how-to I write up for myself after every quest of this
sort. Any suggestions on how to improve this would be appreciated.

Although I'm new to struts I've collected several of these how-to
articles which address problems I've seen on this list. Is there an
archive I can post them where they can be critiqued and shared with
others?

Thanks for all the help.

J.

J. Braun
Polaroid Corp.
Waltham MA USA
+1 (781) 386 6871
[EMAIL PROTECTED]

"Lemmings, as a group, have a very bad reputation. But no individual
lemming has ever been singled out for blame."
Anonymous

/// code /////////////////////////////////////////

How to read an image from a db and display in an html page

Goal: Display an image in a .jsp page that is stored in a database blob

Synopsis: The blob is read into a byte array that is stored in the
ActionForm. The .jsp page uses a <img tag to call an action that writes
the byte array to the response.

Reference: http://struts.apache.org/userGuide/struts-html.html#img

The code:

-- This reads the image in from the db
        try
        {
          pictureStream = rs.getBinaryStream("picture");
          byte[] bytes = new byte[1024*1024]; // some maximum size

          int byteSize = pictureStream.read(bytes); // read in the bytes
          
          byte[] bytesX = new byte[byteSize]; // create a new array of
the proper size
          
          for(int i=0;i<byteSize;i++) // copy them into the new array
          {
            bytesX[i] = bytes[i];
          }
          
          pictureBytes = bytesX; // assign it to the form variable
        }
        catch(FileNotFoundException ex)
        {
          System.err.println("selectionForm.populate.picturefile: " +
ex.getMessage());
          logger.error("selectionForm.populate.picturefile: " +
ex.getMessage());
        }
        catch(IOException ex)
        {
          System.err.println("selectionForm.populate.picturefile: " +
ex.getMessage());
          logger.error("selectionForm.populate.picturefile: " +
ex.getMessage());
        }

-- This is the .jsp code
                        <tr>
                          <td height="210" colspan="2" valign="top"
align="center">
                            <!-- this is the photo section -->
                            <div align="center">
                              <html:img action="/jpegServerAction.do"
width="105" height="142"
alt="Client photo" />
                            </div>
                          </td>
                        </tr>

-- This is the action code that writes the array to the response

public class JpegServerAction
  extends Action
{
  public ActionForward execute(ActionMapping actionMapping, 
                                         ActionForm actionForm,
                               HttpServletRequest request,
                               HttpServletResponse response)
  {
    WL_Logger logger = new WL_Logger();

    SelectionForm selectionForm = (SelectionForm)actionForm;

    try
    {
      response.setContentType("image/jpeg");
      ServletOutputStream out = response.getOutputStream();
      if(selectionForm.getPictureBytes() != null)
      {
        out.write(selectionForm.getPictureBytes());
      }
    }
    catch(IOException ex)
    {
      System.err.println("JpegServerAction.execute: " +
ex.getMessage());
      logger.error("JpegServerAction.execute: " + ex.getMessage());
    }

    return null; // tells the Action servlet to not do anything more
  }
}

-- This is the struts-config.xml entry - scope must be session!

    <action name="selectionForm" 
            path="/jpegServerAction" 
            scope="session" 
            type="transcard15.JpegServerAction" 
            validate="false" />

-- 
This transmission is intended only for use by the addressee(s) named herein and 
may contain information that is proprietary, confidential and/or legally 
privileged. If you are not the intended recipient, you are hereby notified that 
any disclosure, copying, distribution, or use of the information contained 
herein (including any reliance thereon) is STRICTLY PROHIBITED. If you received 
this transmission in error, please immediately contact the sender and destroy 
the material in its entirety, whether in electronic or hard copy format. Thank 
you.



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to