Accessing local files on Tomcat

11,025

You're probably better off looking at Tomcat Aliases. They let you mount external directories in to your web app's context, and have Tomcat serve the file directly.

Addenda:

The part of a URL before the : is called the "scheme". Examples include "http", "https", "ftp", as well as "file".

The scheme tells the browser "what to do" with the rest of the URL, different schemes can have different formats after the :.

So, a URL starting with file:// use the "file" scheme. The file scheme tells the browser to try and open the filename specified by the rest of the URL. That filename is a LOCAL filename. Local to the browser. Not the server.

You would almost never use the file:// type of URL on a server based application, but only on a client side application.

Share:
11,025
KyleCrowley
Author by

KyleCrowley

Updated on June 04, 2022

Comments

  • KyleCrowley
    KyleCrowley almost 2 years

    I'm using Apache Tomcat 7 and I have a servlet where it lists all files in a directory and allows users to download the files by clicking on a link.

    Here is the problem:

    The files that I am accessing in the servlet are NOT in project directory itself. They are in a directory outside of the tomcat directory itself

    I.E.

    Project is in /opt/apache-tomcat-7.0.53 Files are in /files/

    When I use an anchor tag, I can't reference using href="" because that assumes the directory is within the WebContent folder of the project.

    I tried doing href="file:///files/<FILE NAME> but I could not click on the link to download the file so something must be wrong.

    Anyone know how to work around this?

    EDIT: I should also point out this isn't running on my local machine. This is on a server.