方法一:
通过getSession方法获取项目的位置:
String s = req.getSession().getServletContext().getRealPath("\\WEB-INF\\classes\\controller.xml");
String s = req.getSession().getServletContext().getRealPath("\\WEB-INF\\classes\\controller.xml");
File file = new File(s);
FileInputStream in = new FileInputStream(file);
byte [] content = new byte[1024];
int length = in.read(content);
String xml = new String(content,0,length);
因为获取到的是真实路径所以还要通过File等类的处理来或的FileInputstream
方法二:通过获取class字节码文件getClassLoader来实现
InputStream in = aaacc.class.getClassLoader().getResourceAsStream("controller.xml");
因为获取的是InputStream所以要直接根据InputStream进行处理
ufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
String xml = sb.toString();