Search code examples
javascriptjavatomcat8

Using locations outside of tomcat folder to store and access Image files within Tomcat 8 - GNU Linux OS


I am running a webpage + some ws in Tomcat that is running on a GNU Linux OS. The page will display images after a search on the images, the actual images are located within a folder within the webapp/appname folder. Recently the free space within the system has gone short so I asked the sys admin for more space and in response they added a new NetApp volume and mounted it in /data folder. This is outside of the Tomcat folder and I am having difficulty accessing it. Looking around I found this site:

Serving static content (including web pages) from outside of the WAR using Apache Tomcat

but such solution is for when you want to access the image through url, the images I am showing are shown in a slider and point to the folder within tomcat. I have also read that I can create a symlink for all the files, but will this work? will the slider, bootstrap carousel, work? the carousel population is done with javascript.

Does anyone have any idea how to solve this issue?

cheers,

es


Solution

  • After some searching and reading, I founded the solution that works in Tomcat 8. To be able to store and then access images (files) on folders outside of Tomcat folder you need to do the following:

    1- Add the following line in the <host> node within the conf/server.xml:

    <Context docBase="path to the location outside of tomcat" path="path  within tomcat"/>
    

    2- In the conf/context.xml add the following within the <Context> block

    <Resource allowLinking="true">
    

    3- In the application META-INF/context.xml add the above as well, in my case it looks like this

    <?xml version="1.0" encoding0"UTF-8"?>
    <Context path"/myapp">
     <Resource allowLinking="true">
    </Context>
    

    4- for all the files that are located in the outer folder create a symlink, you can run the following command

    ln -s /path to other location/* ./images/
    

    the images is the folder within my web application. After this for each file in the outer location a link will be created within the images folder. The only problem with this solution is that each time you update the application or add a new image you will need to create the symlink(s) again.

    hope this help, cheers

    es.