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 09:37:08 GMT
Looks like roll your own then!

A few thoughts on the matter - maybe someone could add to them?

It should be easy to map requests for images to a servlet, which can
then find the appropriate image file wherever it might be ( within or
outside the server ). Like this in the web.xml file:

<servlet-mapping>
	<servlet-name>servletName</servlet-name>
	<url-pattern>*.jpg</url-pattern>
</servlet-mapping>

?

But how is the image then added to the reponse? Another servlet (
behind the scenes - I am actually using  the Spring framework ) is
handling the request/response. Can the request/response be passed to
the image-providing servlet for the images within a page to be written
to the reponse in this kind of way:

 // This method is called by the servlet container to process a GET request.
    public void doGet(HttpServletRequest req, HttpServletResponse
resp) throws IOException {
        // Get the absolute path of the image
        ServletContext sc = getServletContext();
        String filename = sc.getRealPath("image.gif");

        // Get the MIME type of the image
        String mimeType = sc.getMimeType(filename);
        if (mimeType == null) {
            sc.log("Could not get MIME type of "+filename);
            resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            return;
        }

        // Set content type
        resp.setContentType(mimeType);

        // Set content size
        File file = new File(filename);
        resp.setContentLength((int)file.length());

        // 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
        byte[] buf = new byte[1024];
        int count = 0;
        while ((count = in.read(buf)) >= 0) {
            out.write(buf, 0, count);
        }
        in.close();
        out.close();
    }

( from http://www.exampledepot.com/egs/javax.servlet/GetImage.html )

I'm off for a walk to mull it over - any suggestions while I am out
and before I get to experimenting will be most welcome.

Thanks,

John

---------------------------------------------------------------------
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