classloader重新加载资源

app在tomcat中运行,默认使用的是WebappClassLoader。

InputStream inputStream =Thread.currentThread().getContextClassLoader().getResourceAsStream(“xls/” +
xlsName);

inputStream只要加载过一次后,就会被WebappClassLoader缓存起来,如果需要加载新的资源文件,需要修改为以下方式:

// 因为webappclassloader会缓存加载的资源,所以每次手动去打开文件
			// InputStream inputStream =
			// GameInstance.gameClassLoader.getResourceAsStream("xls/" +
			// xlsName);
			URL xlsURL = GameInstance.gameClassLoader.getResource("xls/" + xlsName);
			URLConnection resConn = xlsURL.openConnection();
			resConn.setUseCaches(false);
			InputStream inputStream = resConn.getInputStream();
发表评论?

0 条评论。

发表评论


注意 - 你可以用以下 HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>


Warning: Use of undefined constant XML - assumed 'XML' (this will throw an Error in a future version of PHP) in /opt/wordpress/wp-content/plugins/wp-syntaxhighlighter/wp-syntaxhighlighter.php on line 1048