getClass().getClassLoader().getResourceAsStream()

2019-04-19 14:53发布

I have a resource (velocity template) which I'd like to be able to swap during development. However,

getClass().getClassLoader().getResourceAsStream() 

seems to cache the template. Is there a way to disable this besides using a file loader instead of the class loader?

2条回答
放荡不羁爱自由
2楼-- · 2019-04-19 15:42

See if something like this helps (exception handling omitted):

URL res = getClass().getClassLoader().getResource(resName);
if (res != null) {
    URLConnection resConn = res.openConnection();
    resConn.setUseCaches(false);
    InputStream in = resConn.getInputStream();
}
查看更多
地球回转人心会变
3楼-- · 2019-04-19 15:47

To avoid caching you can use:

getClass().getClassLoader().getResource().openStream()

It would be equal to using URLResourceLoader for Velocity instead of ClasspathResourceLoader I suppose. I would just go with a file loader.

查看更多
登录 后发表回答