我在一个servlet加载一个文件,使用.getClassLoader().getResourceAsStream(path),路径在WEB-INF/classes目录,我改变路径文件内容后发现,但是文件servlet加载是一样的,不要改变, 文件被缓存。
示例代码:
在我更改 test.key 内容后,此方法每次总是得到相同的结果
private String getKey(String param){
String name = "keys/"+param+"/test.key";
InputStream in = XXXServlet.class.getClassLoader().getResourceAsStream(name);
StringBuilder builder = new StringBuilder();
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line = null;
while((line = reader.readLine()) != null){
builder.append(line).append("\n");
}
} catch (IOException ignoreException) {
}finally{
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
String result = builder.toString();
return result;
}
==================================================== ================
更改这两行代码,它工作正常
String name = "/WEB-INF/classes/keys/"+param+"/test.key";
InputStream in = getServletContext().getResourceAsStream(name);