无法加载resources文件夹中的资源文件(Servlet)

路径没错的情况下可以试试下面这种方法

//        InputStream inputStream = this.getServletContext().getResourceAsStream("/db.properties");
        /**
         * 资源文件resources会被编译成字节码文件
         * 放入服务器的classes文件夹中运行,就没有了resources文件夹
         * 所以这里的路径先得到类加载器,得到db.properties的路径
         */
        String inputStream = ServletDemo05.class.getClassLoader().getResource("db.properties").getPath();
        InputStream is = new FileInputStream(inputStream);
        Properties prop = new Properties();
        prop.load(is);

你可能感兴趣的:(idea,Java,Tomcat)