tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Williams, Allen" <awill...@harris.com>
Subject RE: where to store user-generated files?
Date Wed, 21 Feb 2007 14:45:25 GMT
I'm new at this, so bear with me here for a moment...

The servlet mapping seems to me to tell tomcat "anytime you have a
request for a URI with .jpg extension, deliver the request to this
servlet", but that doesn't give any information about where in the
"real" file system said jpeg is stored, does it?  So, when you call
sc.getRealPath(), how does the servlet context know where to go?
Doesn't there have to be a mapping or alias somewhere (server.xml,
web.xml,...?) that resolves, or translates "ThisTypeofFileName.ext" into
"/real/path/in/OS/ThatTypeOfFileName.oxt"?


-----Original Message-----
From: John Pedersen [mailto:john.pedersen@gmail.com] 
Sent: Wednesday, February 21, 2007 4:37 AM
To: Tomcat Users List
Subject: Re: where to store user-generated files?

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


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