简体   繁体   中英

Serving static content from a jarfile with Jetty?

This should be fairly easy, but for some reason nearly everything I try just seems to hand out a 'not found' error when I hook it up to a web browser.

I've got a single static context, and for the ResourceBase I've got 'file:jar:/path/to/myjar!/.'... any ideas what I'm missing?

Try to load the resource from classloader like this,

    ClassLoader classLoader =
                Thread.currentThread().getContextClassLoader();

        if (classLoader == null) {
            classLoader = getClass().getClassLoader();
        }


        InputStream stream = classLoader.getResourceAsStream(name);

Your approach assumes absolute path and it may not be true when the server is deployed. The jar could be in another JAR (WAR) or a temporary directory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM