Access Local Files or Static Content (Images, Videos, Media or any other files outside of the Web Application(WAR)) in the System using APACHE TOMCAT Server

Access Local Files or Static Content (Images, Videos, Media or any other files outside of the Web Application(WAR)) in the System using APACHE TOMCAT Server

This article details on how to access local directory files(IMAGES, VIDEOS or any other files) from the web application when using APACHE TOMCAT SERVER. Note that this configuration we implement is independent of the web application that we are using.

STEPS TO FOLLOW

  1. Go to TOMCAT directory
  2. Go to  TOMCAT_DIRECTORY/conf/server.xml
  3. Add the following content in server.xml
<Context docBase=”F:/myproj” path=”/static” />

4. Full SERVER.XML file

<?xml version=’1.0′ encoding=’utf-8′?>
<Server port=”8005″ shutdown=”SHUTDOWN”>
<Listener className=”org.apache.catalina.core.AprLifecycleListener” SSLEngine=”on” />
<Listener className=”org.apache.catalina.core.JasperListener” />
<Listener className=”org.apache.catalina.core.JreMemoryLeakPreventionListener” />
<Listener className=”org.apache.catalina.mbeans.GlobalResourcesLifecycleListener” />
<Listener className=”org.apache.catalina.core.ThreadLocalLeakPreventionListener” />
<GlobalNamingResources>
<Resource name=”UserDatabase” auth=”Container”
type=”org.apache.catalina.UserDatabase”
description=”User database that can be updated and saved”
factory=”org.apache.catalina.users.MemoryUserDatabaseFactory”
pathname=”conf/tomcat-users.xml” />
</GlobalNamingResources>
<Service name=”Catalina”>
<Connector port=”8080″ protocol=”HTTP/1.1″
connectionTimeout=”20000″
redirectPort=”8443″ />
<Connector port=”8009″ protocol=”AJP/1.3″ redirectPort=”8443″ />
<Engine name=”Catalina” defaultHost=”localhost”>
<Realm className=”org.apache.catalina.realm.LockOutRealm”>
<Realm className=”org.apache.catalina.realm.UserDatabaseRealm”
resourceName=”UserDatabase”/>
</Realm>
<Host name=”localhost” appBase=”webapps”
unpackWARs=”true” autoDeploy=”true”>
<Context docBase=”F:/myproj” path=”/static” />
<Valve className=”org.apache.catalina.valves.AccessLogValve” directory=”logs”
prefix=”localhost_access_log.” suffix=”.txt”
pattern=”%h %l %u %t &quot;%r&quot; %s %b” />
</Host>
</Engine>
</Service>
</Server>

 

5. With this configuration, you will be able to access the content in F:/myproj folder from the web application i.e the browser.

6. Consider the TOMCAT SERVER is up and running ( localhost:8080 ).

7. The  contents in F:/myproj can be accessed using the URL localhost:8080/static.

8. Example – Consider we have the following files in F:/myproj 

Files – F:/myproj /test.png and F:/myproj /test1.png

URL to Access  – localhost:8080/static/test.png

The configuration will work for TOMCAT version 7 and above(Any Operating System). This configuration helps us to access static content(even web pages) which are located in the local file system of the server.

USE CASE OF THIS TECHNIQUE

If we have a web application where we will adding new images files to the server. In this case we can either add the required files to the web application, build WAR and deploy it in the server or configure the TOMCAT server so that we can add the required files to configured path and access it without building the WAR or restarting the TOMCATserver.

[ YOU MAY ALSO LIKE ]

Leave a Reply