You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Wendy Smoak <We...@asu.edu> on 2004/06/15 23:05:25 UTC

[OT] RE: Rendering Images

> From: CRANFORD, CHRIS [mailto:chris.cranford@setechusa.com] 
> That is what I'd like to do.   Have an image which is 
> rendered in the case
> when the defined image cannot be loaded.  the problem I have 
> is that our
> database record says that an image should exist, but the
> manufacturer/supplier didn't provide it to us... thus I need 
> a way to check
> if that image does exist to test that condition.

How often do you get new images?  If the data is bad, resist programming
around it, and FIX it.  (Pet peeve of mine, I've spent hours writing
around "issues" that were really training/data entry problems.)  Maybe
there's a problem with a procedure when new items are added.  I'm
guessing, of course...

I'd be more inclined to keep a closer eye on the logs (which will show
the 404's when a non-existent resource is requested) and write a
standalone program to run at night daily/weekly/monthly to go through
the database and check that each image is where it's supposed to be.

I use Apache to serve images and other static resources, leaving the
dynamic stuff to Tomcat, but I don't have any idea how to convince
Apache to send an alternate image instead of a 404.

If I had to do it in Tomcat, I'd map a Servlet to the .jpg file
extension, read in the requested image, and either send it or send the
'no image found' image out to the browser.

-- 
Wendy Smoak
Application Systems Analyst, Sr.
ASU IA Information Resources Management 


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: [OT] RE: Rendering Images

Posted by mike <mi...@michaelmcgrady.com>.
I deliver all resources with the following code (you have to fill in some 
details).  You can easily see where you can test for the availablitlity of 
the image.  Anyway, you can always test with, as I said before the exists() 
method of the File class.


public class InitResponse {

   public InitResponse() {
   }

   public String init(HttpServletRequest request,
                      HttpServletResponse response) {
     String fileType = (request.getParameter(ImageConstant.TYPE)).intern();
     String fileName = request.getParameter(ImageConstant.NAME);

     String contentType = null;
     String file = null;
     if (ImageConstant.CSS_FILE.equals(fileType)) {
       contentType = ImageConstant.CSS_CONTENT;
       file = ImageConstant.CSS_LOCATION;
     } else if (ImageConstant.JPEG_FILE.equals(fileType)) {
       contentType = ImageConstant.JPEG_CONTENT;
       file = ImageConstant.JPEG_LOCATION;
     } else if (ImageConstant.PNG_FILE.equals(fileType)) {
       contentType = ImageConstant.PNG_CONTENT;
       file = ImageConstant.PNG_LOCATION;
     } else if (ImageConstant.FLASH_FILE.equals(fileType)) {
       contentType = ImageConstant.FLASH_CONTENT;
       file = ImageConstant.FLASH_LOCATION;
     } else if (ImageConstant.GIF_FILE.equals(fileType)) {
       contentType = ImageConstant.GIF_CONTENT;
       file = ImageConstant.GIF_LOCATION;
     } else if (ImageConstant.TEXT_FILE.equals(fileType)) {
       contentType = ImageConstant.TEXT_CONTENT;
       file = ImageConstant.TEXT_LOCATION;
     } else if (ImageConstant.HTML_FILE.equals(fileType)) {
       contentType = ImageConstant.HTML_CONTENT;
       file = ImageConstant.HTML_LOCATION;
     } else if (ImageConstant.APPLET_FILE.equals(fileType)) {
       contentType = ImageConstant.APPLET_CONTENT;
       file = ImageConstant.APPLET_LOCATION;
     }
     response.setContentType(contentType);
     return Classpath.WEB_INF + file + fileName;
   }
}


public class WriteResponse {

   public WriteResponse() {
   }

   public static void write(String fileName,
                            HttpServletResponse response) {
     response.setHeader("Cache-Control", "");
     response.setHeader("Pragma", "");
     response.setHeader("Expires", "");
     response.addHeader("Content-Disposition","filename=" + fileName);

     try {
       FileInputStream     fis      = new FileInputStream(fileName);
       BufferedInputStream bis      = new BufferedInputStream(fis);
       byte[]              bytes    = new byte[bis.available()];
       OutputStream        os       = response.getOutputStream();
       bis.read(bytes);
       os.write(bytes);
       os.flush();
       os.close();
     } catch (IOException ioe) {
       StdOut.log("log.error","WriteResponse: problem file (B) is: " + 
fileName + "\n" + StackTrace.trace(ioe));
     }
   }
}

At 02:05 PM 6/15/2004, Wendy Smoak wrote:
> > From: CRANFORD, CHRIS [mailto:chris.cranford@setechusa.com]
> > That is what I'd like to do.   Have an image which is
> > rendered in the case
> > when the defined image cannot be loaded.  the problem I have
> > is that our
> > database record says that an image should exist, but the
> > manufacturer/supplier didn't provide it to us... thus I need
> > a way to check
> > if that image does exist to test that condition.
>
>How often do you get new images?  If the data is bad, resist programming
>around it, and FIX it.  (Pet peeve of mine, I've spent hours writing
>around "issues" that were really training/data entry problems.)  Maybe
>there's a problem with a procedure when new items are added.  I'm
>guessing, of course...
>
>I'd be more inclined to keep a closer eye on the logs (which will show
>the 404's when a non-existent resource is requested) and write a
>standalone program to run at night daily/weekly/monthly to go through
>the database and check that each image is where it's supposed to be.
>
>I use Apache to serve images and other static resources, leaving the
>dynamic stuff to Tomcat, but I don't have any idea how to convince
>Apache to send an alternate image instead of a 404.
>
>If I had to do it in Tomcat, I'd map a Servlet to the .jpg file
>extension, read in the requested image, and either send it or send the
>'no image found' image out to the browser.
>
>--
>Wendy Smoak
>Application Systems Analyst, Sr.
>ASU IA Information Resources Management
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>For additional commands, e-mail: user-help@struts.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org