0

I'm running Tomcat 8 on Linux and there I have a folder

/opt/files

where tomcat and other applications should save and access files. However, I don't know how to set this up in Tomcat to make use of these files

user2529173
  • 1,884
  • 6
  • 30
  • 43
  • i didn't get you? – Amit Apr 11 '17 at 15:37
  • On Linux there is this "/opt/files" folder which is outside of the Tomcat folder. Tomcat folder is located at the documents folder in my mac. In this /opt/files folder there are images I want to display in my tomcat app, but that doesn't work with an absolute path in the image tag. So I somehow have to include this /opt/files in my tomcat app – user2529173 Apr 11 '17 at 15:39
  • What files did you want *Tomcat* itself (the container itself, not an webapp installed in Tomcat) to store there? If log files, then you configure location in the logging configuration file. – Andreas Apr 11 '17 at 15:39
  • Sorry, I didn't mean tomcat itself, I mean tomcat apps as well as some other applications – user2529173 Apr 11 '17 at 15:40
  • have you tried with Adding a `` tag in `server.xml`, inside the `` tag:` in tomcat refer [link](http://stackoverflow.com/questions/1552812/mapping-a-directory-outside-the-web-app-to-url-in-tomcat) for more details. – Rajith Pemabandu Apr 11 '17 at 15:40
  • So, it sounds like you want a webapp (not Tomcat) to serve up static files that are not actually packaged with the webapp? Did I understand that right? If so, unpackage the .war file and create symbolic links as necessary. – Andreas Apr 11 '17 at 15:41
  • Yes! That's what I want to do! @RajithPemabandu no I haven't. How do I do this? – user2529173 Apr 11 '17 at 15:42
  • @RajithPemabandu Tomcat discourages the use of `` in `server.xml`. You should instead create context XML files in the `.../conf/Catalina/localhost` folder. Quoting [Tomcat documentation](http://tomcat.apache.org/tomcat-8.5-doc/config/context.html#Defining_a_context): *It is NOT recommended to place elements directly in the server.xml file.* – Andreas Apr 11 '17 at 15:45
  • @Andreas suppose you are correct. this may be a solution. [link](http://stackoverflow.com/questions/23143697/adding-external-resources-to-class-path-in-tomcat-8) – Rajith Pemabandu Apr 11 '17 at 15:49

1 Answers1

-1

A. Use linux soft link is the easiest way

sudo ln -s /opt/files /var/lib/tomcat8/webapps/optfiles

You don't needed change any thing about tomcat.

B. You can also make a soft link to your webapps fold, or even in your webapps/myapp folder (use sudo ln -s /opt/files /var/lib/tomcat8/webapps/myapp/optfiles ), but in this situation you must change context.xml file

 <Context allowLinking="true" >

the allowLinking link attributes make the files accessible, but not recommend for safety reason.

and

C. Depends how you install the tomcat 8, in short, find your tomcat conf direcotry, change the server.xml like below

   <Server ...>

    <Engine>
       ...
      <Service>
     ...
           <Host name="image.hostname.com"  appBase="/opt/files"
                unpackWARs="true" autoDeploy="true"
                xmlValidation="false" xmlNamespaceAware="false">
          </Host>
          <Host name="app.hostname.com"  appBase=""
                unpackWARs="true" autoDeploy="true"
                xmlValidation="false" xmlNamespaceAware="false">
          </Host>

       </Engine>
      </Service>
    </Server>

and now your can access your file through http://your.hostname.com:8080

If you use ubuntu apt-get install your tomcat, you can find the configuration files holds in folder /var/lib/tomcat8/conf or /etc/tomcat8/

Yu Jiaao
  • 4,444
  • 5
  • 44
  • 57
  • According to [comment by OP](http://stackoverflow.com/questions/43350445/tomcat-8-include-external-files#comment73764045_43350445), the `/opt/files` folder only stores some images that webapp needs. Your solution here moves the entire webapp to the folder. That is not what was requested. – Andreas Apr 11 '17 at 15:48