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
- Go to TOMCAT directory
- Go to TOMCAT_DIRECTORY/conf/server.xml
- Add the following content in server.xml
4. Full SERVER.XML file
<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 "%r" %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.
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 http://3.0.59.209/access-local-files-static-content-images-videos-media-files-outside-web-application-system-apache-tomcat-server/
I find if adding the line in sever.xml of Apache Tomcat 9.0
「」will fail to start Tomcat!
I have followed above steps for retrieving the images from outside webapp folder but couldn’t retrieve it .
I have followed above steps for retrieving the images from outside webapp folder and retrieve it .
Thanks
what another changes you have done. I am unable to start the tomcat service. i tried on 9.22 and 8.5 both version.