Search code examples
javatomcatruntimepublish

How to publish non-Java resources generated at runtime on a Tomcat server?


I have a Java webapp running on Tomcat. At runtime, I create images files that I want to be publicly published on the tomcat server.

1/ How can I get the local URL where I want to copy my image files? (ie /mylocalpath/to/where/i/should/store/the/file/)

2/ How can I know the URL where other machines can access this public files? (ie http://mydomainname/myapp/myresource.png)


Solution

  • I've figured a much simpler way to do this (which may sound obvious to Tomcat experts but useful to others).

    In Tomcat 6 "server.xml" file, I've added this line in the <Host> element :

    <Context docBase="/mylocalpath/to/where/i/should/store/the/file" path="/uploads" />
    

    Then, when i create my resource i copy it in this local directory and figure out the public URL pretty easily : http://myserver/uploads/myfilename

    Hope it can help other people.

    (I even think the context can be defined in a context.xml included in the WAR rather than in Tomcat's global configuration but a global definition was enough for my needs).