tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "John Pedersen" <john.peder...@gmail.com>
Subject Re: where to store user-generated files?
Date Wed, 21 Feb 2007 16:48:44 GMT
I have something that works:

in web.xml:

 <servlet>
	<servlet-name>imageServlet</servlet-name>
     	<servlet-class>ImageServlet</servlet-class>
	<load-on-startup>2</load-on-startup>
</servlet>

<servlet-mapping>
     <servlet-name>imageServlet</servlet-name>
     <url-pattern>/users/image/*</url-pattern>
</servlet-mapping>

Links to user images are like this:

<img src="/starfriend/users/image/${match.thumbName}">

( I am using Velocity )

and this is the servlet:

public class ImageServlet extends HttpServlet {

	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		
                String path = PhotoHandler.getUserImageDirectory();		
		String pathInfo = req.getPathInfo();
		String fileName = path + pathInfo;
		
                // Set content type
		resp.setContentType("jpg");

		// Set content size
		File file = new File(fileName);
		resp.setContentLength((int) file.length());
		resp.setContentType("image/jpeg");

		// Open the file and output streams
		FileInputStream in = new FileInputStream(file);
		OutputStream out = resp.getOutputStream();

		// Copy the contents of the file to the output stream
		FileCopyUtils.copy(in, out);
	}

}

Image directory path is held in a properties file, which is held in
memory by a class called PhotoHandler - that is just the way I have it
- of course the path to the image folder could be an init parameter of
the servlet.

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Mime
View raw message