我说一个可以用传统方法来获取
//properties代码 死代码,可以牢记:
Properties pro = new Properties();
pro.load(in);
pro.getProperty(name);- private void proMethod(InputStream in) throws IOException {
- Properties pro = new Properties();
- pro.load(in);
-
- //获取资源
- String url = pro.getProperty("url");
- String username = pro.getProperty("username");
- String password = pro.getProperty("password");
- System.out.println("Current read present resource is:");
- System.out.println(url);
- System.out.println(username + ":" + password);
- }
复制代码 public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String path = this.getServletContext().getRealPath("/WEB-INF/classes/db.properties");
String filename = path.substring(path.lastIndexOf("\\") + 1);/*path.lastIndexOf("\\") return 最后一个\所在的索引 path.substring(path.lastIndexOf("\\"+1) return该字符串索引后的子字符串*/
System.out.println("获取的文件资源为:" + filename);
FileInputStream in = new FileInputStream(path);
proMethod(in);
} |