Here is an example.  You can make the cache values dynamic.  I assume you
know how to use class loaders to get your classpath dynamically, which is
what Classpath.WEB_INF does.  This solution involves having one folder where
you store all your resources, separating them into subfolders named "tiff",
etc.


public final class ResourceCacheAction
   extends Action {

 public ActionForward execute(ActionMapping mapping,
                              ActionForm form,
                              HttpServletRequest request,
                              HttpServletResponse response)
     throws IOException,
            ServletException {
   String file    = request.getParameter("file");
   String ext     = file.substring(file.lastIndexOf('.') + 1);
   String type    = null;
   String path    = null;

   if ("gif".equals(ext)) {
     type = "image/gif";
     path = path("gif");
   } else if ("jpg".equals(ext)) {
     type = "image/jpeg";
     path = path("jpeg");
   } else if ("css".equals(ext)) {
     type = "text/css";
     path = path("css");
   } else if ("flash".equals(ext)) {
     type = "application/x-shockwave-flash";
     path = path("flash");
   } else if ("text".equals(ext)) {
     type = "text/plain";
     path = path("text");
   } else if ("js".equals(ext)) {
     type = "text/javascript";
     path = path("js");
   } else if ("png".equals(ext)) {
     type = "image/png";
     path = path("png");
   } else if ("html".equals(ext)) {
     type = "text/html";
     path = path("html");
   } else if ("bmp".equals(ext)) {
     type = "image/bmp";
     path = path("bmp");
   } else if ("tif".equals(ext)) {
     type = "image/tiff";
     path = path("tiff");
   } else if ("tiff".equals(ext)) {
     type = "image/tiff";
     path = path("tiff");
   }

   String fileName = Classpath.WEB_INF + path + file;

   response.setContentType(type);

   /* E.g. cache = "post-check=120,pre-check=240",
                 = "max-age=86400,private"

      post-checks are the time after the initial cache at which you
      have an immediate view without any update.  The time from the
      post to the pre-check is the time whem you get an immediate
      view but an update.  The time after the pre-check time is when
      you get an update prior to a view.
      Cf. 
http://msdn.microsoft.com/workshop/author/perf/perftips.asp#Use_Cache-Control_Extensions
*/<http://msdn.microsoft.com/workshop/author/perf/perftips.asp#Use_Cache-Control_Extensions*/>

   String cache   = request.getParameter("cache");  /* HTTP 1.1: Numbers
are seconds for cache. */
   String pragma  = request.getParameter("pragma"); /* HTTP 1.0: Numbers
are seconds for pragma. */
   String expires = request.getParameter("expires");/* HTTP 1.0: Numbers
are minutes for expires. */

   response.setHeader("Cache-Control",      ((cache   == null) ?
"max-age=8640000,private" : cache));
   response.setHeader("Pragma",             ((pragma  == null) ?
"max-age=8640000,private" : pragma));
   response.setHeader("Expires",            ((expires == null) ?
"144000"     : 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();
     os = null;
     fis.close();
     fis = null;
     bis.close();
     bis = null;
   } catch (IOException ioe) {
     StdOut.log(SiteConstant.ERROR_LOG,"ResourceCacheAction: problem file
is: " + fileName + "\n" + StackTrace.trace(ioe) + "\n" + ioe.getMessage());
   }

   return null;
 }

 private String path(String fileType) {
   return "resource" + File.separator + "content_type" + File.separator +
fileType + File.separator;
 }
} ///;-)

On 5/16/06, M.Liang Liu <[EMAIL PROTECTED]> wrote:

*Dakota Jack,*your solution is awesome;cool.
Actually I am a freshman to struts;as a result,I need your help to do the
same as you.
**Greatly appreciated if you send your code of the very action
class  which
is to deliver all images or other resources.

Thanks .
On 5/17/06, Dakota Jack <[EMAIL PROTECTED]> wrote:
>
> Using standard URLs is just a guarantee that you will keep having these
> sorts of problems.  I use an action to deliver all images, including
> flash,
> or other resources, CSS, etc.  I would suggest you do the same and you
> will
> never have to worry about this recurrent problem again.
>
>




--
"You can lead a horse to water but you cannot make it float on its back."
~Dakota Jack~

Reply via email to