/** * class LozinskiRandomImage extends HttpServlet - Version 2.0 * * This class is used to generate random images within JSP and Servlets. * * INSTALLATION: * 1) Download this text file to your server and save as "LozinskiRandomImage.java" * 2) Edit the value for the private variable "imageDirectory" from "./gfx/" to whatever * default directory you would like. It should end with a trailing slash "/" character! * 3) Compile the class * 4) Install the class on your server in the specified class-path location * * RUNNING/USING: * 1) You will need to import the compiled class file into your JSP page similar to the following: * <%@ page import="LozinskiRandomImage;" %> * 2) Use the appropriate constructors and methods. * * The random images can be returned one of two ways: * 1) As an HTML IMG tag revealing the name of the random image * OR * 2) As raw binary data when invoked from an HTML IMG tag as a servlet. * * Simple usage examples respectively: * * Example 1) * * <% * : * : * : * LozinskiRandomImage lri = new LozinskiRandomImage(getServletConfig().getServletContext(),"gfx/"); * : * : * : * %> * * Any HTML/JSP tags * * <%= lri.getRandomImageTag() %> * * * Example 2) * * Random Image * * HISTORY: * 06/05/04 2.0 Original release * * AUTHOR: * http://www.davelozinski.com/scripts/ */ import java.io.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; public class LozinskiRandomImage extends HttpServlet { //The URL to the directory where the "Random Images" //are kept. This will only need to be changed if //1) The directory isn't correct // AND EITHER //2) The Constructor is used that doesn't require the directory OR //3) This is called as a servlet private String imageDirectory = "./gfx/"; private ServletContext application; /** * LozinskiRandomImage(ServletContext a, String d) * * Constructor when this class is not used as a servlet. * * First parameter is the ServletContext object passed in. * Second parameter is the relative directory path where the random images are located. * * Examples: * <% LozinskiRandomImage lri = new LozinskiRandomImage(getServletConfig().getServletContext(),"gfx/"); %> * <% LozinskiRandomImage lri = new LozinskiRandomImage(getServletConfig().getServletContext(),"../../RandomImages/"); %> */ public LozinskiRandomImage(ServletContext a, String d) { imageDirectory = d; application = a; } /** * LozinskiRandomImage(ServletContext a) * * Constructor when this class is not used as a servlet. * * Parameter is the ServletContext object passed in. * * Example: * <% LozinskiRandomImage lri = new LozinskiRandomImage(getServletConfig().getServletContext()); %> */ public LozinskiRandomImage(ServletContext a) { application = a; } /** * LozinskiRandomImage() * * Constructor when this class is used as a servlet ONLY, and should not be called. * * Example: * Random Image */ public LozinskiRandomImage() { } //This method does the work when called as a servlet. // //It creates some variables. //Reads the specified directory where the random images are kept, //filtering for gif and jpg images only. // //If images are found, then it responds with the content-type headers, //and finally writes out the binary image data. public void service(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException { application = getServletConfig().getServletContext(); File dir = new File(application.getRealPath(imageDirectory)); byte[] buf = new byte[1024]; int x=0; //Obtain the list of files FilenameFilter imageFiles = new FilenameFilter() { public boolean accept(File path, String name) { return (name.endsWith("gif") || name.endsWith("jpg") || name.endsWith("jpeg")); } }; File[] files = dir.listFiles(imageFiles); if (files.length > 0) { int randomNum = new Random().nextInt(files.length); OutputStream os = response.getOutputStream(); BufferedInputStream in = new BufferedInputStream(new FileInputStream(files[randomNum])); //Print the response headers response.setHeader("Pragma", "Pragma: no-cache"); if (files[randomNum].toString().endsWith("gif")) { response.setContentType("Content-type: image/gif"); } else if (files[randomNum].toString().endsWith("jpg")) { response.setContentType("Content-type: image/jpg"); } else if (files[randomNum].toString().endsWith("jpeg")) { response.setContentType("Content-type: image/jpeg"); } //Write the binary data from the image file while ((x = in.read(buf, 0, buf.length)) != -1) { os.write(buf, 0, x); } os.flush(); os.close(); in.close(); } } //service /** * String getRandomImageTag () * * Returns an HTML IMG tag specifying an image for the URL. * * Otherwise, returns an error message. * * Example: * <% LozinskiRandomImage lri = new LozinskiRandomImage(getServletConfig().getServletContext(),"gfx/"); %> * <%= lri.getRandomImageTag() %> */ public String getRandomImageTag () { File dir = new File(application.getRealPath(imageDirectory)); String msg = "";//#Either the IMG tag or message to be returned. if (dir.isDirectory()) { FilenameFilter imageFiles = new FilenameFilter() { public boolean accept(File path, String name) { return (name.endsWith("gif") || name.endsWith("jpg") || name.endsWith("jpeg")); } }; File[] files = dir.listFiles(imageFiles); if (files.length > 0) { int randomNum = new Random().nextInt(files.length); msg = "\"\""; } else { msg = "No random images located in \"" + dir.toString() + "\""; } } else { msg = "\"" + dir.toString() + "\" does not appear to be a valid directory!"; } return msg; } //getRandomImageTag () /** * String getImageDirectory() * * Returns the current image directory where the random images are stored. * * Example: * <% LozinskiRandomImage lri = new LozinskiRandomImage(getServletConfig().getServletContext()),"gfx/"); %> * <%= lri.getImageDirectory() %> */ public String getImageDirectory() { return imageDirectory; } /** * void setImageDirectory(String d) * * Sets the random image directory to use to the string passed in. * * Example: * LozinskiRandomImage lri = new LozinskiRandomImage(getServletConfig().getServletContext()); * lri.setImageDirectory("gfx/"); * lri.getRandomImageTag(); */ public void setImageDirectory(String d) { imageDirectory = d; } } //LozinskiRandomImage