How to configure Wildfly to serve static content (like images)?

Christian Götz picture Christian Götz · Mar 27, 2014 · Viewed 38.9k times · Source

I have a JavaEE application running on Wildfly 8.0.0 Final.

The application uses a lot of images and I don't want to store them in the database, so they are written to the hard disk.

How can I configure Wildfly/Undertow in order to serve these files (/var/images) on a certain URL, for example http://localhost:8080/myapplication/imagesFromDisk?

Answer

Harald Wellmann picture Harald Wellmann · Mar 27, 2014

Add another file handler and another location to the undertow subsystem in standalone.xml:

<server name="default-server">
    <http-listener name="default" socket-binding="http"/>
    <host name="default-host" alias="localhost">
        <location name="/" handler="welcome-content"/>
        <location name="/img" handler="images"/>
    </host>
</server>
<handlers>
    <file name="welcome-content" path="${jboss.home.dir}/welcome-content" directory-listing="true"/>
    <file name="images" path="/var/images" directory-listing="true"/>
</handlers>