我在src下面放了一个properties文件 用测试类的main方法可以得到 启动Tomcat后在拦截器里面得不到
InputStream inStream = PropertiesUtil.class.getClassLoader().getSystemResourceAsStream(file);
Debug后 发现inStream = null why? 难道上述语句不是加载WEB-INF下的东东?
有没有在不写路径情况下读取到WEB-INF/classes中properties文件的方法public static String getPropertByPath(String filePath ,String fileName ,String propKey) throws IOException{
//构造文件路径
String file = fileName ;
//如果路径为空则默认是src下的文件
if(filePath!=null&&!"".equals(filePath.trim())) file = filePath+"/"+fileName;
//读取文件并构造成文件流
InputStream inStream = PropertiesUtil.class.getClassLoader().getSystemResourceAsStream(file);
//构造属性并装载文件
Properties properties = new Properties();
properties.load(inStream);
return (String)properties.get(propKey);
}
InputStream inStream = PropertiesUtil.class.getClassLoader().getSystemResourceAsStream(file);
Debug后 发现inStream = null why? 难道上述语句不是加载WEB-INF下的东东?
有没有在不写路径情况下读取到WEB-INF/classes中properties文件的方法public static String getPropertByPath(String filePath ,String fileName ,String propKey) throws IOException{
//构造文件路径
String file = fileName ;
//如果路径为空则默认是src下的文件
if(filePath!=null&&!"".equals(filePath.trim())) file = filePath+"/"+fileName;
//读取文件并构造成文件流
InputStream inStream = PropertiesUtil.class.getClassLoader().getSystemResourceAsStream(file);
//构造属性并装载文件
Properties properties = new Properties();
properties.load(inStream);
return (String)properties.get(propKey);
}
fis = new FileInputStream(getPath() + "config.txt");
ps.load(new DataInputStream(fis));
ps.getPropert("dasfdsaf");
这个语句加载的就是web-inf/classes下面的东西了
参考http://blog.csdn.net/frank3g/article/details/3340858
2、过滤器的初始化时,FilterConfig类可以通过getServletContext().getRealPath("WEB-INF/classes/")来取到class路径。