Send reply to:  Tomcat Users List <users@tomcat.apache.org>
Date sent:      Sun, 14 Dec 2008 12:56:17 +0100
From:   Robert Drescher <robert.dresc...@gmail.com>
To:     users@tomcat.apache.org
Subject:        File system resource for static content

> Hello users,
> I'm trying to find the best practice way for implementing the
> following: I want a servlet to perform file uploads and to store the
> files in the local filesystem. This part is not that hard to do as i'm
> currently writing into the javax.servlet.context.tempdir.
> 
> The problem is that i need to store the files in a directory, that
> will be accessible from the web. My tutor at university gave me the
> hint that this is best done with a resource which points to a local
> directory and that's mapped to the webapp.
> 
> So I imagine that http://localhost:8080/WebTest/Upload is my servlet
> mapping and that http://localhost:8080/WebTest/files/ points to this
> resource.
> 

I do something very similar using apache commons upload project.

I have a separate files directory in my webapp directory and I use the 
following code to 
initialize a path to save my uploaded files:

ServletContext sc = getServletContext();
String path = sc.getRealPath("/files");

I can then access a file in the that files directory via

http://server:8080/appname/files/filename


-Steve O.




> But all my research in the documentation did not bring any success. I
> know that i can specify resources in the context.xml, but not how I
> specify the path on the local system or how to do the mapping... If
> anyone already did this and can provide me with configuration
> examples, I'd very much appreciate it.
> 
> I am using Tomcat 5.5 on Linux (not the pre-packaged), the application
> is deployed as war, my context.xml is located in META-INF
> ------------------------------------------------------------ <?xml
> version="1.0" encoding="UTF-8"?> <Context privileged="true"
>          antiResourceLocking="false"
>          antiJARLocking="false"
>          path="WebTest"
> />
> -------------------------------------------------------------
> 
> My web.xml is the following:
> -------------------------------------------------------------
> <?xml version="1.0" encoding="UTF-8"?>
> <web-app version="2.4"
>     xmlns="http://java.sun.com/xml/ns/j2ee";
>     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
>     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
>     http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd";>
> <description>Test Internetapplikationen</description>
> 
>   <servlet>
>     <description>Controller Servlet</description>
>     <display-name>Controller Servlet</display-name>
>     <servlet-name>Controller</servlet-name>
>     <servlet-class>org.agility.webtest.control.Controller</servlet-cla
>     ss>
>   </servlet>
> 
>   <servlet-mapping>
>     <servlet-name>Controller</servlet-name>
>     <url-pattern>/Controller</url-pattern>
>   </servlet-mapping>
>   <welcome-file-list>
>     <welcome-file>Login.jsp</welcome-file>
>   </welcome-file-list>
> </web-app>
> ------------------------------------------------------------------
> 
> Thank you very much for your help
> Robert
> 



---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org

Reply via email to