tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Williams, Allen" <>
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

-----Original Message-----
From: John Pedersen [] 
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:



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

        // Set content type

        // Set content size
        File file = new File(filename);

        // 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 = >= 0) {
            out.write(buf, 0, count);

( from )

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.



To start a new topic, e-mail:
To unsubscribe, e-mail:
For additional commands, e-mail:

To start a new topic, e-mail:
To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message